lol

nixos: Add nixos.channel.enable

For those who wish to get rid of nix-channel.

+145 -14
+43 -10
nixos/modules/config/nix-channel.nix
··· 21 21 { 22 22 options = { 23 23 nix = { 24 + channel = { 25 + enable = mkOption { 26 + description = lib.mdDoc '' 27 + Whether the `nix-channel` command and state files are made available on the machine. 28 + 29 + The following files are initialized when enabled: 30 + - `/nix/var/nix/profiles/per-user/root/channels` 31 + - `/root/.nix-channels` 32 + - `$HOME/.nix-defexpr/channels` (on login) 33 + 34 + Disabling this option will not remove the state files from the system. 35 + ''; 36 + type = types.bool; 37 + default = true; 38 + }; 39 + }; 40 + 24 41 nixPath = mkOption { 25 42 type = types.listOf types.str; 26 - default = [ 27 - "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" 28 - "nixos-config=/etc/nixos/configuration.nix" 29 - "/nix/var/nix/profiles/per-user/root/channels" 30 - ]; 43 + default = 44 + if cfg.channel.enable 45 + then [ 46 + "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" 47 + "nixos-config=/etc/nixos/configuration.nix" 48 + "/nix/var/nix/profiles/per-user/root/channels" 49 + ] 50 + else []; 51 + defaultText = '' 52 + if nix.channel.enable 53 + then [ 54 + "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" 55 + "nixos-config=/etc/nixos/configuration.nix" 56 + "/nix/var/nix/profiles/per-user/root/channels" 57 + ] 58 + else []; 59 + ''; 31 60 description = lib.mdDoc '' 32 61 The default Nix expression search path, used by the Nix 33 62 evaluator to look up paths enclosed in angle brackets ··· 49 78 config = mkIf cfg.enable { 50 79 51 80 environment.extraInit = 52 - '' 81 + mkIf cfg.channel.enable '' 53 82 if [ -e "$HOME/.nix-defexpr/channels" ]; then 54 83 export NIX_PATH="$HOME/.nix-defexpr/channels''${NIX_PATH:+:$NIX_PATH}" 55 84 fi 56 85 ''; 57 86 58 - environment.sessionVariables = { 87 + environment.extraSetup = mkIf (! cfg.channel.enable) '' 88 + rm $out/bin/nix-channel 89 + ''; 90 + 91 + environment.sessionVariables = mkIf (cfg.nixPath != []) { 59 92 NIX_PATH = cfg.nixPath; 60 93 }; 61 94 62 - system.activationScripts.nix-channel = stringAfter [ "etc" "users" ] 63 - '' 95 + system.activationScripts.nix-channel = mkIf cfg.channel.enable 96 + (stringAfter [ "etc" "users" ] '' 64 97 # Subscribe the root user to the NixOS channel by default. 65 98 if [ ! -e "/root/.nix-channels" ]; then 66 99 echo "${config.system.defaultChannel} nixos" > "/root/.nix-channels" 67 100 fi 68 - ''; 101 + ''); 69 102 }; 70 103 }
+82 -4
nixos/tests/installer.nix
··· 11 11 12 12 # The configuration to install. 13 13 makeConfig = { bootLoader, grubDevice, grubIdentifier, grubUseEfi 14 - , extraConfig, forceGrubReinstallCount ? 0 14 + , extraConfig, forceGrubReinstallCount ? 0, flake ? false 15 15 }: 16 16 pkgs.writeText "configuration.nix" '' 17 17 { config, lib, pkgs, modulesPath, ... }: 18 18 19 19 { imports = 20 20 [ ./hardware-configuration.nix 21 - <nixpkgs/nixos/modules/testing/test-instrumentation.nix> 21 + ${if flake 22 + then "" # Still included, but via installer/flake.nix 23 + else "<nixpkgs/nixos/modules/testing/test-instrumentation.nix>"} 22 24 ]; 23 25 24 26 networking.hostName = "thatworked"; ··· 69 71 # partitions and filesystems. 70 72 testScriptFun = { bootLoader, createPartitions, grubDevice, grubUseEfi 71 73 , grubIdentifier, preBootCommands, postBootCommands, extraConfig 72 - , testSpecialisationConfig 74 + , testSpecialisationConfig, testFlakeSwitch 73 75 }: 74 76 let iface = "virtio"; 75 77 isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi); ··· 284 286 285 287 ${postBootCommands} 286 288 machine.shutdown() 289 + '' 290 + + optionalString testFlakeSwitch '' 291 + ${preBootCommands} 292 + machine.start() 293 + 294 + with subtest("Configure system with flake"): 295 + # TODO: evaluate as user? 296 + machine.succeed(""" 297 + mkdir /root/my-config 298 + mv /etc/nixos/hardware-configuration.nix /root/my-config/ 299 + mv /etc/nixos/secret /root/my-config/ 300 + rm /etc/nixos/configuration.nix 301 + """) 302 + machine.copy_from_host_via_shell( 303 + "${ makeConfig { 304 + inherit bootLoader grubDevice grubIdentifier 305 + grubUseEfi extraConfig; 306 + forceGrubReinstallCount = 1; 307 + flake = true; 308 + } 309 + }", 310 + "/root/my-config/configuration.nix", 311 + ) 312 + machine.copy_from_host_via_shell( 313 + "${./installer/flake.nix}", 314 + "/root/my-config/flake.nix", 315 + ) 316 + machine.succeed(""" 317 + # for some reason the image does not have `pkgs.path`, so 318 + # we use readlink to find a Nixpkgs source. 319 + pkgs=$(readlink -f /nix/var/nix/profiles/per-user/root/channels)/nixos 320 + if ! [[ -e $pkgs/pkgs/top-level/default.nix ]]; then 321 + echo 1>&2 "$pkgs does not seem to be a nixpkgs source. Please fix the test so that pkgs points to a nixpkgs source."; 322 + exit 1; 323 + fi 324 + sed -e s^@nixpkgs@^$pkgs^ -i /root/my-config/flake.nix 325 + """) 326 + 327 + with subtest("Switch to flake based config"): 328 + machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") 329 + 330 + ${postBootCommands} 331 + machine.shutdown() 332 + 333 + ${preBootCommands} 334 + machine.start() 335 + 336 + machine.wait_for_unit("multi-user.target") 337 + 338 + with subtest("nix-channel command is not available anymore"): 339 + machine.succeed("! which nix-channel") 340 + 341 + with subtest("Evaluate flake config in fresh env without nix-channel"): 342 + machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") 343 + 344 + with subtest("Evaluate flake config in fresh env without channel profiles"): 345 + machine.succeed(""" 346 + ( 347 + exec 1>&2 348 + rm -v /root/.nix-channels 349 + rm -vrf ~/.nix-defexpr 350 + rm -vrf /nix/var/nix/profiles/per-user/root/channels* 351 + ) 352 + """) 353 + machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") 354 + 355 + ${postBootCommands} 356 + machine.shutdown() 357 + 287 358 ''; 288 359 289 360 ··· 294 365 , grubDevice ? "/dev/vda", grubIdentifier ? "uuid", grubUseEfi ? false 295 366 , enableOCR ? false, meta ? {} 296 367 , testSpecialisationConfig ? false 368 + , testFlakeSwitch ? false 297 369 }: 298 370 makeTest { 299 371 inherit enableOCR; ··· 399 471 testScript = testScriptFun { 400 472 inherit bootLoader createPartitions preBootCommands postBootCommands 401 473 grubDevice grubIdentifier grubUseEfi extraConfig 402 - testSpecialisationConfig; 474 + testSpecialisationConfig testFlakeSwitch; 403 475 }; 404 476 }; 405 477 ··· 451 523 ''; 452 524 }; 453 525 526 + simple-test-config-flake = simple-test-config // { 527 + testFlakeSwitch = true; 528 + }; 529 + 454 530 simple-uefi-grub-config = { 455 531 createPartitions = '' 456 532 machine.succeed( ··· 504 580 # The (almost) simplest partitioning scheme: a swap partition and 505 581 # one big filesystem partition. 506 582 simple = makeInstallerTest "simple" simple-test-config; 583 + 584 + switchToFlake = makeInstallerTest "switch-to-flake" simple-test-config-flake; 507 585 508 586 # Test cloned configurations with the simple grub configuration 509 587 simpleSpecialised = makeInstallerTest "simpleSpecialised" (simple-test-config // specialisation-test-extraconfig);
+20
nixos/tests/installer/flake.nix
··· 1 + # This file gets copied into the installation 2 + 3 + { 4 + # To keep things simple, we'll use an absolute path dependency here. 5 + inputs.nixpkgs.url = "@nixpkgs@"; 6 + 7 + outputs = { nixpkgs, ... }: { 8 + 9 + nixosConfigurations.xyz = nixpkgs.lib.nixosSystem { 10 + modules = [ 11 + ./configuration.nix 12 + ( nixpkgs + "/nixos/modules/testing/test-instrumentation.nix" ) 13 + { 14 + # We don't need nix-channel anymore 15 + nix.channel.enable = false; 16 + } 17 + ]; 18 + }; 19 + }; 20 + }