Skip to content

Commit afbf007

Browse files
committed
tests: add integration test for nh
1 parent 6427258 commit afbf007

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

tests/integration/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let
1313
home-with-symbols = runTest ./standalone/home-with-symbols.nix;
1414
kitty = runTest ./standalone/kitty.nix;
1515
mu = runTest ./standalone/mu;
16+
nh = runTest ./standalone/nh.nix;
1617
nixos-basics = runTest ./nixos/basics.nix;
1718
standalone-flake-basics = runTest ./standalone/flake-basics.nix;
1819
standalone-standard-basics = runTest ./standalone/standard-basics.nix;

tests/integration/standalone/nh.nix

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{ pkgs, ... }:
2+
3+
let
4+
5+
inherit (pkgs.lib) escapeShellArg;
6+
7+
home = "/home/alice";
8+
9+
in {
10+
name = "works-with-nh-stable";
11+
meta.maintainers = [ pkgs.lib.maintainers.rycee ];
12+
13+
nodes.machine = { ... }: {
14+
imports = [ "${pkgs.path}/nixos/modules/installer/cd-dvd/channel.nix" ];
15+
virtualisation.memorySize = 2048;
16+
environment.systemPackages = [ pkgs.nh ];
17+
nix = {
18+
registry.home-manager.to = {
19+
type = "path";
20+
path = ../../..;
21+
};
22+
settings.extra-experimental-features = [ "nix-command" "flakes" ];
23+
};
24+
users.users.alice = {
25+
isNormalUser = true;
26+
description = "Alice Foobar";
27+
password = "foobar";
28+
uid = 1000;
29+
inherit home;
30+
};
31+
};
32+
33+
testScript = ''
34+
import shlex
35+
36+
start_all()
37+
machine.wait_for_unit("network.target")
38+
machine.wait_for_unit("multi-user.target")
39+
40+
home_manager = "${../..}"
41+
42+
def login_as_alice():
43+
machine.wait_until_tty_matches("1", "login: ")
44+
machine.send_chars("alice\n")
45+
machine.wait_until_tty_matches("1", "Password: ")
46+
machine.send_chars("foobar\n")
47+
machine.wait_until_tty_matches("1", "alice\\@machine")
48+
49+
def logout_alice():
50+
machine.send_chars("exit\n")
51+
52+
def alice_cmd(cmd):
53+
cmd = shlex.quote(f"export XDG_RUNTIME_DIR=/run/user/$UID ; {cmd}")
54+
return f"su -l alice --shell /bin/sh -c {cmd}"
55+
56+
def succeed_as_alice(cmd):
57+
return machine.succeed(alice_cmd(cmd))
58+
59+
def fail_as_alice(cmd):
60+
return machine.fail(alice_cmd(cmd))
61+
62+
# Create a persistent login so that Alice has a systemd session.
63+
login_as_alice()
64+
65+
# Set up a home-manager channel.
66+
succeed_as_alice(" ; ".join([
67+
"mkdir -p ${home}/.nix-defexpr/channels",
68+
f"ln -s {home_manager} ${home}/.nix-defexpr/channels/home-manager"
69+
]))
70+
71+
with subtest("Run nh home switch"):
72+
# Copy a configuration to activate.
73+
succeed_as_alice(" ; ".join([
74+
"mkdir -vp ${home}/.config/home-manager",
75+
"cp -v ${
76+
./alice-flake-init.nix
77+
} ${home}/.config/home-manager/flake.nix",
78+
"cp -v ${./alice-home-next.nix} ${home}/.config/home-manager/home.nix"
79+
]))
80+
81+
actual = succeed_as_alice("nh home switch --no-nom '${home}/.config/home-manager'")
82+
expected = "Starting Home Manager activation"
83+
assert expected in actual, \
84+
f"expected nh home switch to contain {expected}, but got {actual}"
85+
86+
# The default configuration creates this link on activation.
87+
machine.succeed("test -L '${home}/.cache/.keep'")
88+
'';
89+
}

0 commit comments

Comments
 (0)