Add the possibility to define assertions to be checked before activation.
This commit is contained in:
parent
02a0e81d6d
commit
c9a47913f4
3 changed files with 105 additions and 2 deletions
45
nix/lib.nix
45
nix/lib.nix
|
|
@ -106,6 +106,50 @@ in
|
|||
${system-manager}/bin/system-manager deactivate "$@"
|
||||
'';
|
||||
|
||||
preActivationAssertionScript =
|
||||
let
|
||||
mkAssertion = { name, script, ... }: ''
|
||||
# ${name}
|
||||
|
||||
echo -e "Evaluating pre-activation assertion ${name}...\n"
|
||||
(
|
||||
set +e
|
||||
${script}
|
||||
)
|
||||
assertion_result=$?
|
||||
|
||||
if [ $assertion_result -ne 0 ]; then
|
||||
failed_assertions+=${name}
|
||||
fi
|
||||
'';
|
||||
|
||||
mkAssertions = assertions:
|
||||
lib.concatStringsSep "\n" (
|
||||
lib.mapAttrsToList (name: mkAssertion) (
|
||||
lib.filterAttrs (name: cfg: cfg.enable)
|
||||
assertions
|
||||
)
|
||||
);
|
||||
in
|
||||
pkgs.writeShellScript "preActivationAssertions" ''
|
||||
set -ou pipefail
|
||||
|
||||
declare -a failed_assertions=()
|
||||
|
||||
${mkAssertions nixosConfig.system-manager.preActivationAssertions}
|
||||
|
||||
if [ ''${#failed_assertions[@]} -ne 0 ]; then
|
||||
for failed_assertion in ''${failed_assertions[@]}; do
|
||||
echo "Pre-activation assertion $failed_assertion failed."
|
||||
done
|
||||
echo "See the output above for more details."
|
||||
exit 1
|
||||
else
|
||||
echo "All pre-activation assertions succeeded."
|
||||
exit 0
|
||||
fi
|
||||
'';
|
||||
|
||||
linkFarmNestedEntryFromDrv = dirs: drv: {
|
||||
name = lib.concatStringsSep "/" (dirs ++ [ "${drv.name}" ]);
|
||||
path = drv;
|
||||
|
|
@ -120,6 +164,7 @@ in
|
|||
(linkFarmBinEntryFromDrv activationScript)
|
||||
(linkFarmBinEntryFromDrv deactivationScript)
|
||||
(linkFarmBinEntryFromDrv registerProfileScript)
|
||||
(linkFarmBinEntryFromDrv preActivationAssertionScript)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,24 @@
|
|||
type = with lib.types; listOf str;
|
||||
default = [ ];
|
||||
};
|
||||
|
||||
preActivationAssertions = lib.mkOption {
|
||||
type = with lib.types; attrsOf (submodule ({ name, ... }: {
|
||||
options = {
|
||||
enable = lib.mkEnableOption "the assertion";
|
||||
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = name;
|
||||
};
|
||||
|
||||
script = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
};
|
||||
};
|
||||
}));
|
||||
default = { };
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
|
|
@ -46,6 +64,27 @@
|
|||
}
|
||||
);
|
||||
|
||||
system-manager.preActivationAssertions = {
|
||||
osVersion =
|
||||
let
|
||||
supportedIds = [ "nixos" "ubuntu" ];
|
||||
in
|
||||
{
|
||||
enable = true;
|
||||
script = ''
|
||||
source /etc/os-release
|
||||
${lib.concatStringsSep "\n" (lib.flip map supportedIds (supportedId: ''
|
||||
if [ $ID = "${supportedId}" ]; then
|
||||
exit 0
|
||||
fi
|
||||
''))}
|
||||
echo "This OS is not currently supported."
|
||||
echo "Supported OSs are: ${lib.concatStringsSep ", " supportedIds}"
|
||||
exit 1
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
# Add the system directory for systemd
|
||||
system-manager.etcFiles = [ "systemd/system" ];
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue