From 732eb3c4ccaac3312fe9334b5d77b3b88140c971 Mon Sep 17 00:00:00 2001 From: Jan Malakhovski Date: Fri, 18 Sep 2015 13:46:47 +0000 Subject: [PATCH 1/2] nixos: cleanup version module, allow setting nixosVersion with env variable --- nixos/modules/misc/version.nix | 48 +++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/nixos/modules/misc/version.nix b/nixos/modules/misc/version.nix index b4b0281fe587..ee6948db3d3a 100644 --- a/nixos/modules/misc/version.nix +++ b/nixos/modules/misc/version.nix @@ -2,13 +2,21 @@ with lib; +let + cfg = config.system; + + releaseFile = "${toString pkgs.path}/.version"; + suffixFile = "${toString pkgs.path}/.version-suffix"; + revisionFile = "${toString pkgs.path}/.git-revision"; +in + { - options = { + options.system = { - system.stateVersion = mkOption { + stateVersion = mkOption { type = types.str; - default = config.system.nixosRelease; + default = cfg.nixosRelease; description = '' Every once in a while, a new NixOS release may change configuration defaults in a way incompatible with stateful @@ -22,38 +30,40 @@ with lib; ''; }; - system.nixosVersion = mkOption { + nixosVersion = mkOption { internal = true; type = types.str; description = "NixOS version."; }; - system.nixosRelease = mkOption { + nixosRelease = mkOption { readOnly = true; type = types.str; - default = readFile "${toString pkgs.path}/.version"; + default = readFile releaseFile; description = "NixOS release."; }; - system.nixosVersionSuffix = mkOption { + nixosVersionSuffix = mkOption { internal = true; type = types.str; + default = if pathExists suffixFile then readFile suffixFile else "pre-git"; description = "NixOS version suffix."; }; - system.nixosRevision = mkOption { + nixosRevision = mkOption { internal = true; type = types.str; + default = if pathExists revisionFile then readFile revisionFile else "master"; description = "NixOS Git revision hash."; }; - system.nixosCodeName = mkOption { + nixosCodeName = mkOption { readOnly = true; type = types.str; description = "NixOS release code name."; }; - system.defaultChannel = mkOption { + defaultChannel = mkOption { internal = true; type = types.str; default = https://nixos.org/channels/nixos-unstable; @@ -64,18 +74,14 @@ with lib; config = { - system.nixosVersion = mkDefault (config.system.nixosRelease + config.system.nixosVersionSuffix); + system = { + # This is set here rather than up there so that changing this + # env variable will not rebuild the manual + nixosVersion = mkDefault (maybeEnv "NIXOS_VERSION" (cfg.nixosRelease + cfg.nixosVersionSuffix)); - system.nixosVersionSuffix = - let suffixFile = "${toString pkgs.path}/.version-suffix"; in - mkDefault (if pathExists suffixFile then readFile suffixFile else "pre-git"); - - system.nixosRevision = - let fn = "${toString pkgs.path}/.git-revision"; in - mkDefault (if pathExists fn then readFile fn else "master"); - - # Note: code names must only increase in alphabetical order. - system.nixosCodeName = "Emu"; + # Note: code names must only increase in alphabetical order. + nixosCodeName = "Emu"; + }; # Generate /etc/os-release. See # http://0pointer.de/public/systemd-man/os-release.html for the From 119c8f91e7f17d2cb95f42985816820600c389cf Mon Sep 17 00:00:00 2001 From: Jan Malakhovski Date: Fri, 18 Sep 2015 16:50:48 +0000 Subject: [PATCH 2/2] nixos: introduce system.nixosLabel option and use it where appropriate Setting nixosVersion to something custom is useful for meaningful GRUB menus and /nix/store paths, but actuallly changing it rebulids the whole system path (because of `nixos-version` script and manual pages). Also, changing it is not a particularly good idea because you can then be differentitated from other NixOS users by a lot of programs that read /etc/os-release. This patch introduces an alternative option that does all you want from nixosVersion, but rebuilds only the very top system level and /etc while using your label in the names of system /nix/store paths, GRUB and other boot loaders' menus, getty greetings and so on. --- .../installer/cd-dvd/installation-cd-base.nix | 2 +- nixos/modules/installer/cd-dvd/iso-image.nix | 2 +- nixos/modules/misc/version.nix | 28 +++++++++++++++++-- nixos/modules/services/ttys/agetty.nix | 20 +++++++++---- nixos/modules/system/activation/top-level.nix | 6 ++-- .../extlinux-conf-builder.sh | 4 +-- nixos/modules/virtualisation/azure-image.nix | 2 +- .../virtualisation/brightbox-image.nix | 2 +- .../virtualisation/google-compute-image.nix | 2 +- .../virtualisation/virtualbox-image.nix | 4 +-- 10 files changed, 52 insertions(+), 20 deletions(-) diff --git a/nixos/modules/installer/cd-dvd/installation-cd-base.nix b/nixos/modules/installer/cd-dvd/installation-cd-base.nix index bc3bd872d2a5..2569860a098f 100644 --- a/nixos/modules/installer/cd-dvd/installation-cd-base.nix +++ b/nixos/modules/installer/cd-dvd/installation-cd-base.nix @@ -16,7 +16,7 @@ with lib; ]; # ISO naming. - isoImage.isoName = "${config.isoImage.isoBaseName}-${config.system.nixosVersion}-${pkgs.stdenv.system}.iso"; + isoImage.isoName = "${config.isoImage.isoBaseName}-${config.system.nixosLabel}-${pkgs.stdenv.system}.iso"; isoImage.volumeID = substring 0 11 "NIXOS_ISO"; diff --git a/nixos/modules/installer/cd-dvd/iso-image.nix b/nixos/modules/installer/cd-dvd/iso-image.nix index fa9cc6fa20b9..b79191e0fd4b 100644 --- a/nixos/modules/installer/cd-dvd/iso-image.nix +++ b/nixos/modules/installer/cd-dvd/iso-image.nix @@ -39,7 +39,7 @@ let DEFAULT boot LABEL boot - MENU LABEL NixOS ${config.system.nixosVersion}${config.isoImage.appendToMenuLabel} + MENU LABEL NixOS ${config.system.nixosLabel}${config.isoImage.appendToMenuLabel} LINUX /boot/bzImage APPEND init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams} INITRD /boot/initrd diff --git a/nixos/modules/misc/version.nix b/nixos/modules/misc/version.nix index ee6948db3d3a..18f270cd531b 100644 --- a/nixos/modules/misc/version.nix +++ b/nixos/modules/misc/version.nix @@ -30,6 +30,29 @@ in ''; }; + nixosLabel = mkOption { + type = types.str; + description = '' + NixOS version name to be used in the names of generated + outputs and boot labels. + + If you ever wanted to influence the labels in your GRUB menu, + this is option is for you. + + Can be set directly or with NIXOS_LABEL + environment variable for nixos-rebuild, + e.g.: + + + #!/bin/sh + today=`date +%Y%m%d` + branch=`(cd nixpkgs ; git branch 2>/dev/null | sed -n '/^\* / { s|^\* ||; p; }')` + revision=`(cd nixpkgs ; git rev-parse HEAD)` + export NIXOS_LABEL="$today.$branch-''${revision:0:7}" + nixos-rebuild switch + ''; + }; + nixosVersion = mkOption { internal = true; type = types.str; @@ -75,8 +98,9 @@ in config = { system = { - # This is set here rather than up there so that changing this - # env variable will not rebuild the manual + # These defaults are set here rather than up there so that + # changing them would not rebuild the manual + nixosLabel = mkDefault (maybeEnv "NIXOS_LABEL" cfg.nixosVersion); nixosVersion = mkDefault (maybeEnv "NIXOS_VERSION" (cfg.nixosRelease + cfg.nixosVersionSuffix)); # Note: code names must only increase in alphabetical order. diff --git a/nixos/modules/services/ttys/agetty.nix b/nixos/modules/services/ttys/agetty.nix index 85ee23c1a3dd..ea7196fc8733 100644 --- a/nixos/modules/services/ttys/agetty.nix +++ b/nixos/modules/services/ttys/agetty.nix @@ -2,6 +2,13 @@ with lib; +let + + autologinArg = optionalString (config.services.mingetty.autologinUser != null) "--autologin ${config.services.mingetty.autologinUser}"; + gettyCmd = extraArgs: "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login ${autologinArg} ${extraArgs}"; + +in + { ###### interface @@ -21,9 +28,9 @@ with lib; greetingLine = mkOption { type = types.str; - default = ''<<< Welcome to NixOS ${config.system.nixosVersion} (\m) - \l >>>''; description = '' Welcome line printed by mingetty. + The default shows current NixOS version label, machine type and tty. ''; }; @@ -55,10 +62,11 @@ with lib; ###### implementation - config = let - autologinArg = optionalString (config.services.mingetty.autologinUser != null) "--autologin ${config.services.mingetty.autologinUser}"; - gettyCmd = extraArgs: "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login ${autologinArg} ${extraArgs}"; - in { + config = { + # Note: this is set here rather than up there so that changing + # nixosLabel would not rebuild manual pages + services.mingetty.greetingLine = mkDefault ''<<< Welcome to NixOS ${config.system.nixosLabel} (\m) - \l >>>''; + systemd.services."getty@" = { serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud %I 115200,38400,9600 $TERM"; restartIfChanged = false; @@ -81,7 +89,7 @@ with lib; { serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud console 115200,38400,9600 $TERM"; serviceConfig.Restart = "always"; restartIfChanged = false; - enable = mkDefault config.boot.isContainer; + enable = mkDefault config.boot.isContainer; }; environment.etc = singleton diff --git a/nixos/modules/system/activation/top-level.nix b/nixos/modules/system/activation/top-level.nix index 81088a56fb12..1c242c88863d 100644 --- a/nixos/modules/system/activation/top-level.nix +++ b/nixos/modules/system/activation/top-level.nix @@ -67,7 +67,7 @@ let echo -n "$configurationName" > $out/configuration-name echo -n "systemd ${toString config.systemd.package.interfaceVersion}" > $out/init-interface-version - echo -n "$nixosVersion" > $out/nixos-version + echo -n "$nixosLabel" > $out/nixos-version echo -n "$system" > $out/system mkdir $out/fine-tune @@ -101,7 +101,7 @@ let if [] == failed then pkgs.stdenv.mkDerivation { name = let hn = config.networking.hostName; nn = if (hn != "") then hn else "unnamed"; - in "nixos-system-${nn}-${config.system.nixosVersion}"; + in "nixos-system-${nn}-${config.system.nixosLabel}"; preferLocalBuild = true; allowSubstitutes = false; buildCommand = systemBuilder; @@ -115,7 +115,7 @@ let config.system.build.installBootLoader or "echo 'Warning: do not know how to make this configuration bootable; please enable a boot loader.' 1>&2; true"; activationScript = config.system.activationScripts.script; - nixosVersion = config.system.nixosVersion; + nixosLabel = config.system.nixosLabel; configurationName = config.boot.loader.grub.configurationName; diff --git a/nixos/modules/system/boot/loader/generic-extlinux-compatible/extlinux-conf-builder.sh b/nixos/modules/system/boot/loader/generic-extlinux-compatible/extlinux-conf-builder.sh index b9a42b2a196d..78a8e8fd658c 100644 --- a/nixos/modules/system/boot/loader/generic-extlinux-compatible/extlinux-conf-builder.sh +++ b/nixos/modules/system/boot/loader/generic-extlinux-compatible/extlinux-conf-builder.sh @@ -83,7 +83,7 @@ addEntry() { timestampEpoch=$(stat -L -c '%Z' $path) timestamp=$(date "+%Y-%m-%d %H:%M" -d @$timestampEpoch) - nixosVersion="$(cat $path/nixos-version)" + nixosLabel="$(cat $path/nixos-version)" extraParams="$(cat $path/kernel-params)" echo @@ -91,7 +91,7 @@ addEntry() { if [ "$tag" = "default" ]; then echo " MENU LABEL NixOS - Default" else - echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosVersion)" + echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosLabel)" fi echo " LINUX ../nixos/$(basename $kernel)" echo " INITRD ../nixos/$(basename $initrd)" diff --git a/nixos/modules/virtualisation/azure-image.nix b/nixos/modules/virtualisation/azure-image.nix index 1013396c0498..7c4db45a859b 100644 --- a/nixos/modules/virtualisation/azure-image.nix +++ b/nixos/modules/virtualisation/azure-image.nix @@ -26,7 +26,7 @@ in ${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vpc $diskImage $out/disk.vhd rm $diskImage ''; - diskImageBase = "nixos-image-${config.system.nixosVersion}-${pkgs.stdenv.system}.raw"; + diskImageBase = "nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.raw"; buildInputs = [ pkgs.utillinux pkgs.perl ]; exportReferencesGraph = [ "closure" config.system.build.toplevel ]; diff --git a/nixos/modules/virtualisation/brightbox-image.nix b/nixos/modules/virtualisation/brightbox-image.nix index 0eb46d39b521..b6b2bd4f69be 100644 --- a/nixos/modules/virtualisation/brightbox-image.nix +++ b/nixos/modules/virtualisation/brightbox-image.nix @@ -26,7 +26,7 @@ in rm $diskImageBase popd ''; - diskImageBase = "nixos-image-${config.system.nixosVersion}-${pkgs.stdenv.system}.raw"; + diskImageBase = "nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.raw"; buildInputs = [ pkgs.utillinux pkgs.perl ]; exportReferencesGraph = [ "closure" config.system.build.toplevel ]; diff --git a/nixos/modules/virtualisation/google-compute-image.nix b/nixos/modules/virtualisation/google-compute-image.nix index f21ddc12ca5a..77074b882468 100644 --- a/nixos/modules/virtualisation/google-compute-image.nix +++ b/nixos/modules/virtualisation/google-compute-image.nix @@ -30,7 +30,7 @@ in rm $out/disk.raw popd ''; - diskImageBase = "nixos-image-${config.system.nixosVersion}-${pkgs.stdenv.system}.raw"; + diskImageBase = "nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.raw"; buildInputs = [ pkgs.utillinux pkgs.perl ]; exportReferencesGraph = [ "closure" config.system.build.toplevel ]; diff --git a/nixos/modules/virtualisation/virtualbox-image.nix b/nixos/modules/virtualisation/virtualbox-image.nix index 425726333c40..da9e75a003ad 100644 --- a/nixos/modules/virtualisation/virtualbox-image.nix +++ b/nixos/modules/virtualisation/virtualbox-image.nix @@ -44,8 +44,8 @@ in { system.build.virtualBoxOVA = pkgs.runCommand "virtualbox-ova" { buildInputs = [ pkgs.linuxPackages.virtualbox ]; - vmName = "NixOS ${config.system.nixosVersion} (${pkgs.stdenv.system})"; - fileName = "nixos-image-${config.system.nixosVersion}-${pkgs.stdenv.system}.ova"; + vmName = "NixOS ${config.system.nixosLabel} (${pkgs.stdenv.system})"; + fileName = "nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.ova"; } '' echo "creating VirtualBox VM..."