nixos/activation-script: Add lib.sh with warn()

Changed files
+78
nixos
modules
tests
+2
nixos/modules/system/activation/activation-script.nix
···
''
#!${pkgs.runtimeShell}
+
source ${./lib/lib.sh}
+
systemConfig='@out@'
export PATH=/empty
+5
nixos/modules/system/activation/lib/lib.sh
···
+
# shellcheck shell=bash
+
+
warn() {
+
printf "\033[1;35mwarning:\033[0m %s\n" "$*" >&2
+
}
+36
nixos/modules/system/activation/lib/test.nix
···
+
# Run:
+
# nix-build -A nixosTests.activation-lib
+
{ lib, stdenv, testers }:
+
let
+
inherit (lib) fileset;
+
+
runTests = stdenv.mkDerivation {
+
name = "tests-activation-lib";
+
src = fileset.toSource {
+
root = ./.;
+
fileset = fileset.unions [
+
./lib.sh
+
./test.sh
+
];
+
};
+
buildPhase = ":";
+
doCheck = true;
+
postUnpack = ''
+
patchShebangs --build .
+
'';
+
checkPhase = ''
+
./test.sh
+
'';
+
installPhase = ''
+
touch $out
+
'';
+
};
+
+
runShellcheck = testers.shellcheck {
+
src = runTests.src;
+
};
+
+
in
+
lib.recurseIntoAttrs {
+
inherit runTests runShellcheck;
+
}
+34
nixos/modules/system/activation/lib/test.sh
···
+
#!/usr/bin/env bash
+
+
# Run:
+
# ./test.sh
+
# or:
+
# nix-build -A nixosTests.activation-lib
+
+
cd "$(dirname "${BASH_SOURCE[0]}")"
+
set -euo pipefail
+
+
# report failure
+
onerr() {
+
set +e
+
# find failed statement
+
echo "call trace:"
+
local i=0
+
while t="$(caller $i)"; do
+
line="${t%% *}"
+
file="${t##* }"
+
echo " $file:$line" >&2
+
((i++))
+
done
+
# red
+
printf "\033[1;31mtest failed\033[0m\n" >&2
+
exit 1
+
}
+
trap onerr ERR
+
+
source ./lib.sh
+
+
(warn hi, this works >/dev/null) 2>&1 | grep -E $'.*warning:.* hi, this works' >/dev/null
+
+
# green
+
printf "\033[1;32mok\033[0m\n"
+1
nixos/tests/all-tests.nix
···
esphome = handleTest ./esphome.nix {};
etc = pkgs.callPackage ../modules/system/etc/test.nix { inherit evalMinimalConfig; };
activation = pkgs.callPackage ../modules/system/activation/test.nix { };
+
activation-lib = pkgs.callPackage ../modules/system/activation/lib/test.nix { };
activation-var = runTest ./activation/var.nix;
activation-nix-channel = runTest ./activation/nix-channel.nix;
activation-etc-overlay-mutable = runTest ./activation/etc-overlay-mutable.nix;