nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 expect,
4 nix,
5 nixos-rebuild,
6 path,
7 runCommand,
8 stdenv,
9 writeText,
10}:
11let
12 # Arguably not true, but it holds up for now.
13 escapeExpect = lib.strings.escapeNixString;
14
15 expectSetup = ''
16 set timeout 180
17 proc expect_simple { pattern } {
18 puts "Expecting: $pattern"
19 expect {
20 timeout {
21 puts "\nTimeout waiting for: $pattern\n"
22 exit 1
23 }
24 $pattern
25 }
26 }
27 '';
28
29 # In case we want/need to evaluate packages or the assertions or whatever,
30 # we want to have a linux system.
31 # TODO: make the non-flake test use thise.
32 linuxSystem = lib.replaceStrings [ "darwin" ] [ "linux" ] stdenv.hostPlatform.system;
33
34in
35runCommand "test-nixos-rebuild-repl"
36 {
37 nativeBuildInputs = [
38 expect
39 nix
40 nixos-rebuild
41 ];
42 nixpkgs = if builtins.pathExists (path + "/.git") then lib.cleanSource path else path;
43 }
44 ''
45 export HOME=$(mktemp -d)
46 export TEST_ROOT=$PWD/test-tmp
47
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
55 export PAGER=cat
56 mkdir -p $TEST_ROOT $NIX_CONF_DIR
57
58 echo General setup
59 ##################
60
61 export NIX_PATH=nixpkgs=$nixpkgs:nixos-config=$HOME/configuration.nix
62 cat >> ~/configuration.nix <<EOF
63 {
64 boot.loader.grub.enable = false;
65 fileSystems."/".device = "x";
66 imports = [ ./hardware-configuration.nix ];
67 }
68 EOF
69
70 echo '{ }' > ~/hardware-configuration.nix
71
72
73 echo Test traditional NixOS configuration
74 #########################################
75
76 expect ${writeText "test-nixos-rebuild-repl-expect" ''
77 ${expectSetup}
78 spawn nixos-rebuild repl --fast
79
80 expect "nix-repl> "
81
82 send "config.networking.hostName\n"
83 expect "\"nixos\""
84 ''}
85
86
87 echo Test flake based NixOS configuration
88 #########################################
89
90 # Switch to flake flavored environment
91 unset NIX_PATH
92 cat > $NIX_CONF_DIR/nix.conf <<EOF
93 experimental-features = nix-command flakes
94 EOF
95
96 # Make the config pure
97 echo '{ nixpkgs.hostPlatform = "${linuxSystem}"; }' > ~/hardware-configuration.nix
98
99 cat >~/flake.nix <<EOF
100 {
101 inputs.nixpkgs.url = "path:$nixpkgs";
102 outputs = { nixpkgs, ... }: {
103 nixosConfigurations.testconf = nixpkgs.lib.nixosSystem {
104 modules = [
105 ./configuration.nix
106 # Let's change it up a bit
107 { networking.hostName = "itsme"; }
108 ];
109 };
110 };
111 }
112 EOF
113
114 # cat -n ~/flake.nix
115
116 expect ${writeText "test-nixos-rebuild-repl-absolute-path-expect" ''
117 ${expectSetup}
118 spawn sh -c "nixos-rebuild repl --fast --flake path:\$HOME#testconf"
119
120 expect_simple "nix-repl>"
121
122 send "config.networking.hostName\n"
123 expect_simple "itsme"
124
125 expect_simple "nix-repl>"
126 send "lib.version\n"
127 expect_simple ${
128 escapeExpect (
129 # The version string is a bit different in the flake lib, so we expect a prefix and ignore the rest
130 # Furthermore, including the revision (suffix) would cause unnecessary rebuilds.
131 # Note that a length of 4 only matches e.g. "24.
132 lib.strings.substring 0 4 (lib.strings.escapeNixString lib.version)
133 )
134 }
135
136 # Make sure it's the right lib - should be the flake lib, not Nixpkgs lib.
137 expect_simple "nix-repl>"
138 send "lib?nixosSystem\n"
139 expect_simple "true"
140 expect_simple "nix-repl>"
141 send "lib?nixos\n"
142 expect_simple "true"
143 ''}
144
145 pushd "$HOME"
146 expect ${writeText "test-nixos-rebuild-repl-relative-path-expect" ''
147 ${expectSetup}
148 spawn sh -c "nixos-rebuild repl --fast --flake .#testconf"
149
150 expect_simple "nix-repl>"
151
152 send "config.networking.hostName\n"
153 expect_simple "itsme"
154 ''}
155 popd
156
157 echo
158
159 #########
160 echo Done
161 touch $out
162 ''