summary refs log tree commit diff
path: root/nixos/modules/config
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2023-07-18 15:27:24 +0200
committerGitHub <noreply@github.com>2023-07-18 15:27:24 +0200
commit8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71 (patch)
tree05e8546bf8e001344e080a014fd438503c19c249 /nixos/modules/config
parent28293b2fe1a98d29c3c27d9f8b5839406c564147 (diff)
parenta1d0ee8c505472625e1c48a73843447269878bf0 (diff)
downloadnixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.tar
nixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.tar.gz
nixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.tar.bz2
nixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.tar.lz
nixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.tar.xz
nixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.tar.zst
nixpkgs-8ad59ed1b2c41509e1ec7420bfd7bb18937aaa71.zip
Merge pull request #242098 from hercules-ci/nixos-no-nix-channel
nixos: Disable nix-channel
Diffstat (limited to 'nixos/modules/config')
-rw-r--r--nixos/modules/config/nix-channel.nix56
1 files changed, 47 insertions, 9 deletions
diff --git a/nixos/modules/config/nix-channel.nix b/nixos/modules/config/nix-channel.nix
index 557f17d8b3b..8f6caaabde8 100644
--- a/nixos/modules/config/nix-channel.nix
+++ b/nixos/modules/config/nix-channel.nix
@@ -9,6 +9,7 @@
 { config, lib, ... }:
 let
   inherit (lib)
+    mkDefault
     mkIf
     mkOption
     stringAfter
@@ -21,13 +22,42 @@ in
 {
   options = {
     nix = {
+      channel = {
+        enable = mkOption {
+          description = lib.mdDoc ''
+            Whether the `nix-channel` command and state files are made available on the machine.
+
+            The following files are initialized when enabled:
+             - `/nix/var/nix/profiles/per-user/root/channels`
+             - `/root/.nix-channels`
+             - `$HOME/.nix-defexpr/channels` (on login)
+
+            Disabling this option will not remove the state files from the system.
+          '';
+          type = types.bool;
+          default = true;
+        };
+      };
+
       nixPath = mkOption {
         type = types.listOf types.str;
-        default = [
-          "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
-          "nixos-config=/etc/nixos/configuration.nix"
-          "/nix/var/nix/profiles/per-user/root/channels"
-        ];
+        default =
+          if cfg.channel.enable
+          then [
+            "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
+            "nixos-config=/etc/nixos/configuration.nix"
+            "/nix/var/nix/profiles/per-user/root/channels"
+          ]
+          else [];
+        defaultText = ''
+          if nix.channel.enable
+          then [
+            "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
+            "nixos-config=/etc/nixos/configuration.nix"
+            "/nix/var/nix/profiles/per-user/root/channels"
+          ]
+          else [];
+        '';
         description = lib.mdDoc ''
           The default Nix expression search path, used by the Nix
           evaluator to look up paths enclosed in angle brackets
@@ -49,22 +79,30 @@ in
   config = mkIf cfg.enable {
 
     environment.extraInit =
-      ''
+      mkIf cfg.channel.enable ''
         if [ -e "$HOME/.nix-defexpr/channels" ]; then
           export NIX_PATH="$HOME/.nix-defexpr/channels''${NIX_PATH:+:$NIX_PATH}"
         fi
       '';
 
+    environment.extraSetup = mkIf (!cfg.channel.enable) ''
+      rm $out/bin/nix-channel
+    '';
+
+    # NIX_PATH has a non-empty default according to Nix docs, so we don't unset
+    # it when empty.
     environment.sessionVariables = {
       NIX_PATH = cfg.nixPath;
     };
 
-    system.activationScripts.nix-channel = stringAfter [ "etc" "users" ]
-      ''
+    nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault "");
+
+    system.activationScripts.nix-channel = mkIf cfg.channel.enable
+      (stringAfter [ "etc" "users" ] ''
         # Subscribe the root user to the NixOS channel by default.
         if [ ! -e "/root/.nix-channels" ]; then
             echo "${config.system.defaultChannel} nixos" > "/root/.nix-channels"
         fi
-      '';
+      '');
   };
 }