11 # Arguably not true, but it holds up for now.
12 escapeExpect = lib.strings.escapeNixString;
16 proc expect_simple { pattern } {
17 puts "Expecting: $pattern"
20 puts "\nTimeout waiting for: $pattern\n"
28 # In case we want/need to evaluate packages or the assertions or whatever,
29 # we want to have a linux system.
30 # TODO: make the non-flake test use thise.
31 linuxSystem = lib.replaceStrings ["darwin"] ["linux"] stdenv.hostPlatform.system;
34 runCommand "test-nixos-rebuild-repl" {
41 if builtins.pathExists (path + "/.git")
42 then lib.cleanSource path
45 export HOME=$(mktemp -d)
46 export TEST_ROOT=$PWD/test-tmp
48 # Prepare for running Nix in sandbox
49 export NIX_BUILD_HOOK=
50 export NIX_CONF_DIR=$TEST_ROOT/etc
51 export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
52 export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
53 export NIX_STATE_DIR=$TEST_ROOT/var/nix
54 export NIX_STORE_DIR=$TEST_ROOT/store
56 mkdir -p $TEST_ROOT $NIX_CONF_DIR
61 export NIX_PATH=nixpkgs=$nixpkgs:nixos-config=$HOME/configuration.nix
62 cat >> ~/configuration.nix <<EOF
64 boot.loader.grub.enable = false;
65 fileSystems."/".device = "x";
66 imports = [ ./hardware-configuration.nix ];
70 echo '{ }' > ~/hardware-configuration.nix
73 echo Test traditional NixOS configuration
74 #########################################
76 expect ${writeText "test-nixos-rebuild-repl-expect" ''
78 spawn nixos-rebuild repl --fast
82 send "config.networking.hostName\n"
87 echo Test flake based NixOS configuration
88 #########################################
90 # Switch to flake flavored environment
92 cat > $NIX_CONF_DIR/nix.conf <<EOF
93 experimental-features = nix-command flakes
96 # Make the config pure
97 echo '{ nixpkgs.hostPlatform = "${linuxSystem}"; }' > ~/hardware-configuration.nix
99 cat >~/flake.nix <<EOF
101 inputs.nixpkgs.url = "path:$nixpkgs";
102 outputs = { nixpkgs, ... }: {
103 nixosConfigurations.testconf = nixpkgs.lib.nixosSystem {
106 # Let's change it up a bit
107 { networking.hostName = "itsme"; }
116 expect ${writeText "test-nixos-rebuild-repl-absolute-path-expect" ''
118 spawn sh -c "nixos-rebuild repl --fast --flake path:\$HOME#testconf"
120 expect_simple "nix-repl>"
122 send "config.networking.hostName\n"
123 expect_simple "itsme"
125 expect_simple "nix-repl>"
127 expect_simple ${escapeExpect (
128 # The version string is a bit different in the flake lib, so we expect a prefix and ignore the rest
129 # Furthermore, including the revision (suffix) would cause unnecessary rebuilds.
130 # Note that a length of 4 only matches e.g. "24.
131 lib.strings.substring 0 4 (lib.strings.escapeNixString lib.version))}
133 # Make sure it's the right lib - should be the flake lib, not Nixpkgs lib.
134 expect_simple "nix-repl>"
135 send "lib?nixosSystem\n"
137 expect_simple "nix-repl>"
143 expect ${writeText "test-nixos-rebuild-repl-relative-path-expect" ''
145 spawn sh -c "nixos-rebuild repl --fast --flake .#testconf"
147 expect_simple "nix-repl>"
149 send "config.networking.hostName\n"
150 expect_simple "itsme"