From 35a2c6b33d49bd73c6e1fad09e7aa972cbf22d8b Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sun, 6 Sep 2020 23:49:50 +0000 Subject: [PATCH] 3p/nixpkgs: allow disabling nix in nixos --- .../nixos/modules/installer/tools/tools.nix | 2 +- .../modules/services/misc/nix-daemon.nix | 10 +- .../nixos/modules/services/misc/nix-gc.nix | 2 +- .../modules/services/misc/nix-optimise.nix | 2 +- .../systemd-boot/systemd-boot-builder.py | 21 ++-- .../boot/loader/systemd-boot/systemd-boot.nix | 2 - .../0001-nixpkgs-allow-disable-nix.patch | 108 ++++++++++++++++++ 7 files changed, 131 insertions(+), 16 deletions(-) create mode 100644 third_party/nixpkgs/patches/0001-nixpkgs-allow-disable-nix.patch diff --git a/third_party/nixpkgs/nixos/modules/installer/tools/tools.nix b/third_party/nixpkgs/nixos/modules/installer/tools/tools.nix index 1582f04930..abfa2b9264 100644 --- a/third_party/nixpkgs/nixos/modules/installer/tools/tools.nix +++ b/third_party/nixpkgs/nixos/modules/installer/tools/tools.nix @@ -91,7 +91,7 @@ in ''; }; - config = { + config = mkIf config.nix.enable { system.nixos-generate-config.configuration = mkDefault '' # Edit this configuration file to define what should be installed on diff --git a/third_party/nixpkgs/nixos/modules/services/misc/nix-daemon.nix b/third_party/nixpkgs/nixos/modules/services/misc/nix-daemon.nix index 924a007efc..d6a437ad65 100644 --- a/third_party/nixpkgs/nixos/modules/services/misc/nix-daemon.nix +++ b/third_party/nixpkgs/nixos/modules/services/misc/nix-daemon.nix @@ -81,6 +81,14 @@ in nix = { + enable = mkOption { + type = types.bool; + default = true; + description = '' + If disabled, Nix will not be available on the built NixOS system. + ''; + }; + package = mkOption { type = types.package; default = pkgs.nix; @@ -498,7 +506,7 @@ in ###### implementation - config = { + config = mkIf cfg.enable { assertions = [ { diff --git a/third_party/nixpkgs/nixos/modules/services/misc/nix-gc.nix b/third_party/nixpkgs/nixos/modules/services/misc/nix-gc.nix index 12bed05757..f5faf357c3 100644 --- a/third_party/nixpkgs/nixos/modules/services/misc/nix-gc.nix +++ b/third_party/nixpkgs/nixos/modules/services/misc/nix-gc.nix @@ -48,7 +48,7 @@ in ###### implementation - config = { + config = mkIf config.nix.enable { systemd.services.nix-gc = { description = "Nix Garbage Collector"; diff --git a/third_party/nixpkgs/nixos/modules/services/misc/nix-optimise.nix b/third_party/nixpkgs/nixos/modules/services/misc/nix-optimise.nix index e02026d5f7..129d9c8e39 100644 --- a/third_party/nixpkgs/nixos/modules/services/misc/nix-optimise.nix +++ b/third_party/nixpkgs/nixos/modules/services/misc/nix-optimise.nix @@ -36,7 +36,7 @@ in ###### implementation - config = { + config = mkIf config.nix.enable { systemd.services.nix-optimise = { description = "Nix Store Optimiser"; diff --git a/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py b/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py index 97e824fe62..13a5af9452 100644 --- a/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py +++ b/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py @@ -124,18 +124,19 @@ def mkdir_p(path): raise def get_generations(profile=None): - gen_list = subprocess.check_output([ - "@nix@/bin/nix-env", - "--list-generations", - "-p", - "/nix/var/nix/profiles/%s" % ("system-profiles/" + profile if profile else "system"), - "--option", "build-users-group", ""], - universal_newlines=True) - gen_lines = gen_list.split('\n') - gen_lines.pop() + generation_dir = "/nix/var/nix/profiles/%s" % ("system-profiles" if profile else "",) + profile_name = profile if profile else "system" + generations = [] + for gen_entry in os.scandir(generation_dir): + gen_name = gen_entry.name + if not (gen_name.startswith(profile_name + '-') and gen_name.endswith('-link')): + continue + gen_num = gen_name[len(profile_name+'-'):-len('-link')] + generations.append(int(gen_num)) + generations.sort() configurationLimit = @configurationLimit@ - return [ (profile, int(line.split()[0])) for line in gen_lines ][-configurationLimit:] + return [ (profile, gen_num) for gen_num in sorted(generations) ][-configurationLimit:] def remove_old_entries(gens): rex_profile = re.compile("^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$") diff --git a/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix b/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix index f0bd76a3c1..96d5a6cd51 100644 --- a/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix +++ b/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix @@ -16,8 +16,6 @@ let systemd = config.systemd.package; - nix = config.nix.package.out; - timeout = if config.boot.loader.timeout != null then config.boot.loader.timeout else ""; editor = if cfg.editor then "True" else "False"; diff --git a/third_party/nixpkgs/patches/0001-nixpkgs-allow-disable-nix.patch b/third_party/nixpkgs/patches/0001-nixpkgs-allow-disable-nix.patch new file mode 100644 index 0000000000..b954af114c --- /dev/null +++ b/third_party/nixpkgs/patches/0001-nixpkgs-allow-disable-nix.patch @@ -0,0 +1,108 @@ +diff --git a/nixos/modules/installer/tools/tools.nix b/nixos/modules/installer/tools/tools.nix +--- a/nixos/modules/installer/tools/tools.nix ++++ b/nixos/modules/installer/tools/tools.nix +@@ -86,7 +86,7 @@ in + ''; + }; + +- config = { ++ config = mkIf config.nix.enable { + + system.nixos-generate-config.configuration = mkDefault '' + # Edit this configuration file to define what should be installed on +diff --git a/nixos/modules/services/misc/nix-daemon.nix b/nixos/modules/services/misc/nix-daemon.nix +--- a/nixos/modules/services/misc/nix-daemon.nix ++++ b/nixos/modules/services/misc/nix-daemon.nix +@@ -81,6 +81,14 @@ in + + nix = { + ++ enable = mkOption { ++ type = types.bool; ++ default = true; ++ description = '' ++ If disabled, Nix will not be available on the built NixOS system. ++ ''; ++ }; ++ + package = mkOption { + type = types.package; + default = pkgs.nix; +@@ -498,7 +506,7 @@ in + + ###### implementation + +- config = { ++ config = mkIf cfg.enable { + + nix.binaryCachePublicKeys = [ "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" ]; + nix.binaryCaches = [ "https://cache.nixos.org/" ]; +diff --git a/nixos/modules/services/misc/nix-gc.nix b/nixos/modules/services/misc/nix-gc.nix +--- a/nixos/modules/services/misc/nix-gc.nix ++++ b/nixos/modules/services/misc/nix-gc.nix +@@ -48,7 +48,7 @@ in + + ###### implementation + +- config = { ++ config = mkIf config.nix.enable { + + systemd.services.nix-gc = + { description = "Nix Garbage Collector"; +diff --git a/nixos/modules/services/misc/nix-optimise.nix b/nixos/modules/services/misc/nix-optimise.nix +--- a/nixos/modules/services/misc/nix-optimise.nix ++++ b/nixos/modules/services/misc/nix-optimise.nix +@@ -36,7 +36,7 @@ in + + ###### implementation + +- config = { ++ config = mkIf config.nix.enable { + + systemd.services.nix-optimise = + { description = "Nix Store Optimiser"; +diff --git a/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py b/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py +--- a/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py ++++ b/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py +@@ -124,18 +124,19 @@ def mkdir_p(path): + raise + + def get_generations(profile=None): +- gen_list = subprocess.check_output([ +- "@nix@/bin/nix-env", +- "--list-generations", +- "-p", +- "/nix/var/nix/profiles/%s" % ("system-profiles/" + profile if profile else "system"), +- "--option", "build-users-group", ""], +- universal_newlines=True) +- gen_lines = gen_list.split('\n') +- gen_lines.pop() ++ generation_dir = "/nix/var/nix/profiles/%s" % ("system-profiles" if profile else "",) ++ profile_name = profile if profile else "system" ++ generations = [] ++ for gen_entry in os.scandir(generation_dir): ++ gen_name = gen_entry.name ++ if not (gen_name.startswith(profile_name + '-') and gen_name.endswith('-link')): ++ continue ++ gen_num = gen_name[len(profile_name+'-'):-len('-link')] ++ generations.append(int(gen_num)) ++ generations.sort() + + configurationLimit = @configurationLimit@ +- return [ (profile, int(line.split()[0])) for line in gen_lines ][-configurationLimit:] ++ return [ (profile, gen_num) for gen_num in sorted(generations) ][-configurationLimit:] + + def remove_old_entries(gens): + rex_profile = re.compile("^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$") +diff --git a/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix b/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix +--- a/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix ++++ b/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix +@@ -16,8 +16,6 @@ let + + systemd = config.systemd.package; + +- nix = config.nix.package.out; +- + timeout = if config.boot.loader.timeout != null then config.boot.loader.timeout else ""; + + editor = if cfg.editor then "True" else "False";