From a5992ad61b314104aff7e28a41ce101a1b0e7c35 Mon Sep 17 00:00:00 2001 From: "Nicolas B. Pierron" Date: Wed, 18 Nov 2015 00:26:00 +0000 Subject: [PATCH] Add a way to pin a NixOS version within the module system. This modification add a way to re-evaluate the module system with a different version of NixOS, or with a different set of arguments. --- nixos/default.nix | 16 +++- .../manual/configuration/configuration.xml | 1 + nixos/doc/manual/default.nix | 1 + .../doc/manual/release-notes/rl-unstable.xml | 20 +++++ nixos/modules/misc/nixos.nix | 82 ++++++++++++++++++ nixos/modules/misc/nixos.xml | 84 +++++++++++++++++++ nixos/modules/module-list.nix | 1 + nixos/release.nix | 1 + nixos/tests/nixos-pin-version.nix | 57 +++++++++++++ 9 files changed, 259 insertions(+), 4 deletions(-) create mode 100644 nixos/modules/misc/nixos.nix create mode 100644 nixos/modules/misc/nixos.xml create mode 100644 nixos/tests/nixos-pin-version.nix diff --git a/nixos/default.nix b/nixos/default.nix index 5d69b79e13a..6359d10c880 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -1,12 +1,20 @@ { configuration ? import ./lib/from-env.nix "NIXOS_CONFIG" , system ? builtins.currentSystem +, extraModules ? [] + # This attribute is used to specify a different nixos version, a different + # system or additional modules which might be set conditionally. +, reEnter ? false }: let + reEnterModule = { + config.nixos.path = with (import ../lib); mkIf reEnter (mkForce null); + config.nixos.configuration = configuration; + }; eval = import ./lib/eval-config.nix { inherit system; - modules = [ configuration ]; + modules = [ configuration reEnterModule ] ++ extraModules; }; inherit (eval) pkgs; @@ -14,14 +22,14 @@ let # This is for `nixos-rebuild build-vm'. vmConfig = (import ./lib/eval-config.nix { inherit system; - modules = [ configuration ./modules/virtualisation/qemu-vm.nix ]; + modules = [ configuration reEnterModule ./modules/virtualisation/qemu-vm.nix ] ++ extraModules; }).config; # This is for `nixos-rebuild build-vm-with-bootloader'. vmWithBootLoaderConfig = (import ./lib/eval-config.nix { inherit system; modules = - [ configuration + [ configuration reEnterModule ./modules/virtualisation/qemu-vm.nix { virtualisation.useBootLoader = true; } ]; @@ -30,7 +38,7 @@ let in { - inherit (eval) config options; + inherit (eval.config.nixos.reflect) config options; system = eval.config.system.build.toplevel; diff --git a/nixos/doc/manual/configuration/configuration.xml b/nixos/doc/manual/configuration/configuration.xml index 8fde0dc7e61..afffd60bc48 100644 --- a/nixos/doc/manual/configuration/configuration.xml +++ b/nixos/doc/manual/configuration/configuration.xml @@ -26,6 +26,7 @@ effect after you run nixos-rebuild. + diff --git a/nixos/doc/manual/default.nix b/nixos/doc/manual/default.nix index 87964e27bb9..844cba57cd8 100644 --- a/nixos/doc/manual/default.nix +++ b/nixos/doc/manual/default.nix @@ -55,6 +55,7 @@ let cp -prd $sources/* . # */ chmod -R u+w . cp ${../../modules/services/databases/postgresql.xml} configuration/postgresql.xml + cp ${../../modules/misc/nixos.xml} configuration/nixos.xml ln -s ${optionsDocBook} options-db.xml echo "${version}" > version ''; diff --git a/nixos/doc/manual/release-notes/rl-unstable.xml b/nixos/doc/manual/release-notes/rl-unstable.xml index 573b99d4902..65aa36586cb 100644 --- a/nixos/doc/manual/release-notes/rl-unstable.xml +++ b/nixos/doc/manual/release-notes/rl-unstable.xml @@ -6,6 +6,26 @@ Unstable +In addition to numerous new and upgraded packages, this release +has the following highlights: + + + + + You can now pin a specific version of NixOS in your configuration.nix + by setting: + + +nixos.path = ./nixpkgs-unstable-2015-12-06/nixos; + + + This will make NixOS re-evaluate your configuration with the modules of + the specified NixOS version at the given path. For more details, see + + + + + When upgrading from a previous release, please be aware of the following incompatible changes: diff --git a/nixos/modules/misc/nixos.nix b/nixos/modules/misc/nixos.nix new file mode 100644 index 00000000000..356129211d0 --- /dev/null +++ b/nixos/modules/misc/nixos.nix @@ -0,0 +1,82 @@ +{ config, options, lib, ... }: + +# This modules is used to inject a different NixOS version as well as its +# argument such that one can pin a specific version with the versionning +# system of the configuration. +let + nixosReentry = import config.nixos.path { + inherit (config.nixos) configuration extraModules; + inherit (config.nixpkgs) system; + reEnter = true; + }; +in + +with lib; + +{ + options = { + nixos.path = mkOption { + default = null; + example = literalExample "./nixpkgs-15.09/nixos"; + type = types.nullOr types.path; + description = '' + This option give the ability to evaluate the current set of modules + with a different version of NixOS. This option can be used version + the version of NixOS with the configuration without relying on the + NIX_PATH environment variable. + ''; + }; + + nixos.system = mkOption { + example = "i686-linux"; + type = types.uniq types.str; + description = '' + Name of the system used to compile NixOS. + ''; + }; + + nixos.extraModules = mkOption { + default = []; + example = literalExample "mkIf config.services.openssh.enable [ ./sshd-config.nix ]"; + type = types.listOf types.unspecified; + description = '' + Define additional modules which would be loaded to evaluate the + configuration. + ''; + }; + + nixos.configuration = mkOption { + type = types.unspecified; + internal = true; + description = '' + Option used by nixos/default.nix to re-inject + the same configuration module as the one used for the current + execution. + ''; + }; + + nixos.reflect = mkOption { + default = { inherit config options; }; + type = types.unspecified; + internal = true; + description = '' + Provides config and options + computed by the module system and given as argument to all + modules. These are used for introspection of options and + configuration by tools such as nixos-option. + ''; + }; + }; + + config = mkMerge [ + (mkIf (config.nixos.path != null) (mkForce { + system.build.toplevel = nixosReentry.system; + system.build.vm = nixosReentry.vm; + nixos.reflect = { inherit (nixosReentry) config options; }; + })) + + { meta.maintainers = singleton lib.maintainers.pierron; + meta.doc = ./nixos.xml; + } + ]; +} diff --git a/nixos/modules/misc/nixos.xml b/nixos/modules/misc/nixos.xml new file mode 100644 index 00000000000..f8d3b4bc6e3 --- /dev/null +++ b/nixos/modules/misc/nixos.xml @@ -0,0 +1,84 @@ + + +NixOS Reentry + + + + +Source: modules/misc/nixos.nix + + + +NixOS reentry can be used for both pinning the evaluation to a +specific version of NixOS, and to dynamically add additional modules into +the Module evaluation. + +
NixOS Version Pinning + +To pin a specific version of NixOS, you need a version that you can +either clone localy, or that you can fetch remotely. + +If you already have a cloned version of NixOS in the directory +/etc/nixos/nixpkgs-16-03, then you can specify the + with either the path or the relative path of +your NixOS clone. For example, you can add the following to your +/etc/nixos/configuration.nix file: + + +nixos.path = ./nixpkgs-16-03/nixos; + + + +Another option is to fetch a specific version of NixOS, with either +the fetchTarball builtin, or the +pkgs.fetchFromGithub function and use the result as an +input. + + +nixos.path = "${builtins.fetchTarball https://github.com/NixOS/nixpkgs/archive/1f27976e03c15183191d1b4aa1a40d1f14666cd2.tar.gz}/nixos"; + + + +
+ + +
Adding Module Dynamically + +To add additional module, the recommended way is to use statically +known modules in the list of imported arguments as described in . Unfortunately, this recommended method has +limitation, such that the list of imported files cannot be selected based on +the content of the configuration. + +Fortunately, NixOS reentry system can be used as an alternative to register +new imported modules based on the content of the configuration. To do so, +one should define both and + options. + + +nixos.path = <nixos>; +nixos.extraModules = + if config.networking.hostName == "server" then + [ ./server.nix ] else [ ./client.nix ]; + + +Also note, that the above can be reimplemented in a different way which is +not as expensive, by using mkIf at the top each +configuration if both modules are present on the file system (see ) and by always inmporting both +modules. + +
+ +
Options + +FIXME: auto-generated list of module options. + +
+ + +
diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 034ea933a7d..242571d41ba 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -52,6 +52,7 @@ ./misc/lib.nix ./misc/locate.nix ./misc/meta.nix + ./misc/nixos.nix ./misc/nixpkgs.nix ./misc/passthru.nix ./misc/version.nix diff --git a/nixos/release.nix b/nixos/release.nix index 1a1ed4bca41..e48954ceaf5 100644 --- a/nixos/release.nix +++ b/nixos/release.nix @@ -276,6 +276,7 @@ in rec { tests.networkingProxy = callTest tests/networking-proxy.nix {}; tests.nfs3 = callTest tests/nfs.nix { version = 3; }; tests.nfs4 = callTest tests/nfs.nix { version = 4; }; + tests.nixosPinVersion = callTest tests/nixos-pin-version.nix {}; tests.nsd = callTest tests/nsd.nix {}; tests.openssh = callTest tests/openssh.nix {}; tests.panamax = hydraJob (import tests/panamax.nix { system = "x86_64-linux"; }); diff --git a/nixos/tests/nixos-pin-version.nix b/nixos/tests/nixos-pin-version.nix new file mode 100644 index 00000000000..91fba2e759d --- /dev/null +++ b/nixos/tests/nixos-pin-version.nix @@ -0,0 +1,57 @@ +{ system ? builtins.currentSystem }: + +with import ../lib/testing.nix { inherit system; }; +let +in + +pkgs.stdenv.mkDerivation rec { + name = "nixos-pin-version"; + src = ../..; + buildInputs = with pkgs; [ nix gnugrep ]; + + withoutPath = pkgs.writeText "configuration.nix" '' + { + nixos.extraModules = [ ({lib, ...}: { system.nixosRevision = lib.mkForce "ABCDEF"; }) ]; + } + ''; + + withPath = pkgs.writeText "configuration.nix" '' + { + nixos.path = ${src}/nixos ; + nixos.extraModules = [ ({lib, ...}: { system.nixosRevision = lib.mkForce "ABCDEF"; }) ]; + } + ''; + + phases = "buildPhase"; + buildPhase = '' + datadir="${pkgs.nix}/share" + export TEST_ROOT=$(pwd)/test-tmp + export NIX_STORE_DIR=$TEST_ROOT/store + export NIX_LOCALSTATE_DIR=$TEST_ROOT/var + export NIX_LOG_DIR=$TEST_ROOT/var/log/nix + export NIX_STATE_DIR=$TEST_ROOT/var/nix + export NIX_DB_DIR=$TEST_ROOT/db + export NIX_CONF_DIR=$TEST_ROOT/etc + export NIX_MANIFESTS_DIR=$TEST_ROOT/var/nix/manifests + export NIX_BUILD_HOOK= + export PAGER=cat + cacheDir=$TEST_ROOT/binary-cache + nix-store --init + + export NIX_PATH="nixpkgs=$src:nixos=$src/nixos:nixos-config=${withoutPath}" ; + if test $(nix-instantiate $src/nixos -A config.system.nixosRevision --eval-only) != '"ABCDEF"' ; then :; + else + echo "Unexpected re-entry without the nixos.path option defined."; + exit 1; + fi; + + export NIX_PATH="nixpkgs=$src:nixos=$src/nixos:nixos-config=${withPath}" ; + if test $(nix-instantiate $src/nixos -A config.system.nixosRevision --eval-only) = '"ABCDEF"' ; then :; + else + echo "Expected a re-entry when the nixos.path option is defined."; + exit 1; + fi; + + touch $out; + ''; +}