From f6ddd253e0fdcd359f21b7cb52736fcfe63c187a Mon Sep 17 00:00:00 2001 From: Chuck Date: Mon, 15 Feb 2021 14:40:54 -0800 Subject: [PATCH 1/2] nixos/activation-script: Add a test --- nixos/tests/all-tests.nix | 1 + nixos/tests/user-activation-scripts.nix | 33 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 nixos/tests/user-activation-scripts.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 444580bc0bed..df10b8c0e7b9 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -406,6 +406,7 @@ in unit-php = handleTest ./web-servers/unit-php.nix {}; upnp = handleTest ./upnp.nix {}; usbguard = handleTest ./usbguard.nix {}; + user-activation-scripts = handleTest ./user-activation-scripts.nix {}; uwsgi = handleTest ./uwsgi.nix {}; v2ray = handleTest ./v2ray.nix {}; vault = handleTest ./vault.nix {}; diff --git a/nixos/tests/user-activation-scripts.nix b/nixos/tests/user-activation-scripts.nix new file mode 100644 index 000000000000..4abae3b9d356 --- /dev/null +++ b/nixos/tests/user-activation-scripts.nix @@ -0,0 +1,33 @@ +import ./make-test-python.nix ({ lib, ... }: { + name = "user-activation-scripts"; + meta = with lib.maintainers; { maintainers = [ chkno ]; }; + + machine = { + system.userActivationScripts.foo = "mktemp ~/user-activation-ran.XXXXXX"; + users.users.alice = { + initialPassword = "pass1"; + isNormalUser = true; + }; + }; + + testScript = '' + def verify_user_activation_run_count(n): + machine.succeed( + '[[ "$(find /home/alice/ -name user-activation-ran.\\* | wc -l)" == %s ]]' % n + ) + + + machine.wait_for_unit("multi-user.target") + machine.wait_for_unit("getty@tty1.service") + machine.wait_until_tty_matches(1, "login: ") + machine.send_chars("alice\n") + machine.wait_until_tty_matches(1, "Password: ") + machine.send_chars("pass1\n") + machine.send_chars("touch login-ok\n") + machine.wait_for_file("/home/alice/login-ok") + verify_user_activation_run_count(0) # Wrong! Should be 1 + + machine.succeed("/run/current-system/bin/switch-to-configuration test") + verify_user_activation_run_count(1) # Wrong! Should be 2 + ''; +}) From df25ca8520548431a3a7e85a7863655e76081d39 Mon Sep 17 00:00:00 2001 From: Chuck Date: Mon, 15 Feb 2021 15:09:30 -0800 Subject: [PATCH 2/2] nixos/activation-script: Run userActivationScripts at login Fixes https://github.com/NixOS/nixpkgs/issues/113240 --- nixos/modules/system/activation/activation-script.nix | 1 + nixos/tests/user-activation-scripts.nix | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/nixos/modules/system/activation/activation-script.nix b/nixos/modules/system/activation/activation-script.nix index 3a6930314b1a..47313234d0bc 100644 --- a/nixos/modules/system/activation/activation-script.nix +++ b/nixos/modules/system/activation/activation-script.nix @@ -234,6 +234,7 @@ in script = config.system.userActivationScripts.script; unitConfig.ConditionUser = "!@system"; serviceConfig.Type = "oneshot"; + wantedBy = [ "default.target" ]; }; }; }; diff --git a/nixos/tests/user-activation-scripts.nix b/nixos/tests/user-activation-scripts.nix index 4abae3b9d356..0de8664c5ef0 100644 --- a/nixos/tests/user-activation-scripts.nix +++ b/nixos/tests/user-activation-scripts.nix @@ -25,9 +25,9 @@ import ./make-test-python.nix ({ lib, ... }: { machine.send_chars("pass1\n") machine.send_chars("touch login-ok\n") machine.wait_for_file("/home/alice/login-ok") - verify_user_activation_run_count(0) # Wrong! Should be 1 + verify_user_activation_run_count(1) machine.succeed("/run/current-system/bin/switch-to-configuration test") - verify_user_activation_run_count(1) # Wrong! Should be 2 + verify_user_activation_run_count(2) ''; })