Skip to content

Commit

Permalink
nixos: Add nixos.channel.enable
Browse files Browse the repository at this point in the history
For those who wish to get rid of nix-channel.
  • Loading branch information
roberth committed Jul 7, 2023
1 parent faa1b3b commit d00e242
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 14 deletions.
53 changes: 43 additions & 10 deletions nixos/modules/config/nix-channel.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,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
Expand All @@ -49,22 +78,26 @@ 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.sessionVariables = {
environment.extraSetup = mkIf (! cfg.channel.enable) ''
rm $out/bin/nix-channel
'';

environment.sessionVariables = mkIf (cfg.nixPath != []) {
NIX_PATH = cfg.nixPath;
};

system.activationScripts.nix-channel = stringAfter [ "etc" "users" ]
''
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
'';
'');
};
}
86 changes: 82 additions & 4 deletions nixos/tests/installer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ let

# The configuration to install.
makeConfig = { bootLoader, grubDevice, grubIdentifier, grubUseEfi
, extraConfig, forceGrubReinstallCount ? 0
, extraConfig, forceGrubReinstallCount ? 0, flake ? false
}:
pkgs.writeText "configuration.nix" ''
{ config, lib, pkgs, modulesPath, ... }:
{ imports =
[ ./hardware-configuration.nix
<nixpkgs/nixos/modules/testing/test-instrumentation.nix>
${if flake
then "" # Still included, but via installer/flake.nix
else "<nixpkgs/nixos/modules/testing/test-instrumentation.nix>"}
];
networking.hostName = "thatworked";
Expand Down Expand Up @@ -69,7 +71,7 @@ let
# partitions and filesystems.
testScriptFun = { bootLoader, createPartitions, grubDevice, grubUseEfi
, grubIdentifier, preBootCommands, postBootCommands, extraConfig
, testSpecialisationConfig
, testSpecialisationConfig, testFlakeSwitch
}:
let iface = "virtio";
isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi);
Expand Down Expand Up @@ -284,6 +286,75 @@ let
${postBootCommands}
machine.shutdown()
''
+ optionalString testFlakeSwitch ''
${preBootCommands}
machine.start()
with subtest("Configure system with flake"):
# TODO: evaluate as user?
machine.succeed("""
mkdir /root/my-config
mv /etc/nixos/hardware-configuration.nix /root/my-config/
mv /etc/nixos/secret /root/my-config/
rm /etc/nixos/configuration.nix
""")
machine.copy_from_host_via_shell(
"${ makeConfig {
inherit bootLoader grubDevice grubIdentifier
grubUseEfi extraConfig;
forceGrubReinstallCount = 1;
flake = true;
}
}",
"/root/my-config/configuration.nix",
)
machine.copy_from_host_via_shell(
"${./installer/flake.nix}",
"/root/my-config/flake.nix",
)
machine.succeed("""
# for some reason the image does not have `pkgs.path`, so
# we use readlink to find a Nixpkgs source.
pkgs=$(readlink -f /nix/var/nix/profiles/per-user/root/channels)/nixos
if ! [[ -e $pkgs/pkgs/top-level/default.nix ]]; then
echo 1>&2 "$pkgs does not seem to be a nixpkgs source. Please fix the test so that pkgs points to a nixpkgs source.";
exit 1;
fi
sed -e s^@nixpkgs@^$pkgs^ -i /root/my-config/flake.nix
""")
with subtest("Switch to flake based config"):
machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz")
${postBootCommands}
machine.shutdown()
${preBootCommands}
machine.start()
machine.wait_for_unit("multi-user.target")
with subtest("nix-channel command is not available anymore"):
machine.succeed("! which nix-channel")
with subtest("Evaluate flake config in fresh env without nix-channel"):
machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz")
with subtest("Evaluate flake config in fresh env without channel profiles"):
machine.succeed("""
(
exec 1>&2
rm -v /root/.nix-channels
rm -vrf ~/.nix-defexpr
rm -vrf /nix/var/nix/profiles/per-user/root/channels*
)
""")
machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz")
${postBootCommands}
machine.shutdown()
'';


Expand All @@ -294,6 +365,7 @@ let
, grubDevice ? "/dev/vda", grubIdentifier ? "uuid", grubUseEfi ? false
, enableOCR ? false, meta ? {}
, testSpecialisationConfig ? false
, testFlakeSwitch ? false
}:
makeTest {
inherit enableOCR;
Expand Down Expand Up @@ -399,7 +471,7 @@ let
testScript = testScriptFun {
inherit bootLoader createPartitions preBootCommands postBootCommands
grubDevice grubIdentifier grubUseEfi extraConfig
testSpecialisationConfig;
testSpecialisationConfig testFlakeSwitch;
};
};

Expand Down Expand Up @@ -451,6 +523,10 @@ let
'';
};

simple-test-config-flake = simple-test-config // {
testFlakeSwitch = true;
};

simple-uefi-grub-config = {
createPartitions = ''
machine.succeed(
Expand Down Expand Up @@ -505,6 +581,8 @@ in {
# one big filesystem partition.
simple = makeInstallerTest "simple" simple-test-config;

switchToFlake = makeInstallerTest "switch-to-flake" simple-test-config-flake;

# Test cloned configurations with the simple grub configuration
simpleSpecialised = makeInstallerTest "simpleSpecialised" (simple-test-config // specialisation-test-extraconfig);

Expand Down
20 changes: 20 additions & 0 deletions nixos/tests/installer/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file gets copied into the installation

{
# To keep things simple, we'll use an absolute path dependency here.
inputs.nixpkgs.url = "@nixpkgs@";

outputs = { nixpkgs, ... }: {

nixosConfigurations.xyz = nixpkgs.lib.nixosSystem {
modules = [
./configuration.nix
( nixpkgs + "/nixos/modules/testing/test-instrumentation.nix" )
{
# We don't need nix-channel anymore
nix.channel.enable = false;
}
];
};
};
}

0 comments on commit d00e242

Please sign in to comment.