2022-10-25 23:35:29 +03:00
|
|
|
{ config, pkgs, lib, extendModules, ... }@args:
|
2022-09-05 19:12:09 +03:00
|
|
|
with lib;
|
|
|
|
let
|
2022-10-22 20:37:44 +03:00
|
|
|
options.test.shellcheckServices = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2022-10-22 20:37:44 +03:00
|
|
|
Whether to shellcheck services during system build time.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
sourcePrefix = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2022-10-22 20:37:44 +03:00
|
|
|
The definition source path prefix of services to include in the shellcheck.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
runShellcheck = mkOption {
|
2022-09-05 19:12:09 +03:00
|
|
|
readOnly = true;
|
2022-12-18 15:13:47 +03:00
|
|
|
description = mdDoc ''
|
2022-09-05 19:12:09 +03:00
|
|
|
A derivation that runs shellcheck on all bash scripts included
|
|
|
|
in nix-bitcoin services.
|
|
|
|
'';
|
|
|
|
default = shellcheckServices;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-22 20:37:44 +03:00
|
|
|
cfg = config.test.shellcheckServices;
|
|
|
|
|
|
|
|
# A list of all service names that are defined in source paths prefixed by
|
|
|
|
# `sourcePrefix`.
|
2022-09-05 19:12:09 +03:00
|
|
|
# [ "bitcoind", "clightning", ... ]
|
|
|
|
#
|
2022-10-25 23:35:29 +03:00
|
|
|
# Algorithm: Parse defintions of `systemd.services` and return all services
|
2022-10-22 20:37:44 +03:00
|
|
|
# that only have definitions located within `sourcePrefix`.
|
|
|
|
servicesToCheck = let
|
|
|
|
inherit (cfg) sourcePrefix;
|
2022-10-25 23:35:29 +03:00
|
|
|
systemdServices = args.options.systemd.services;
|
2022-10-25 23:35:30 +03:00
|
|
|
configSystemdServices = args.config.systemd.services;
|
2022-10-22 20:37:44 +03:00
|
|
|
matchingServices = collectServices true;
|
|
|
|
nonMatchingServices = collectServices false;
|
2022-09-05 19:12:09 +03:00
|
|
|
# Return set of services ({ service1 = true; service2 = true; ... })
|
2022-10-22 20:37:44 +03:00
|
|
|
# which are either defined or not defined within `sourcePrefix`, depending
|
|
|
|
# on `shouldMatch`.
|
2022-12-18 15:13:45 +03:00
|
|
|
collectServices = shouldMatch: lib.listToAttrs (builtins.concatLists (map (def:
|
2022-09-05 19:12:09 +03:00
|
|
|
let
|
2022-12-18 15:13:45 +03:00
|
|
|
services = def.value;
|
|
|
|
inherit (def) file;
|
2022-10-22 20:37:44 +03:00
|
|
|
isMatching = lib.hasPrefix sourcePrefix file;
|
2022-09-05 19:12:09 +03:00
|
|
|
in
|
2022-10-25 23:35:29 +03:00
|
|
|
# Nix has no boolean XOR, so use `if`
|
2022-10-22 20:37:44 +03:00
|
|
|
lib.optionals (if shouldMatch then isMatching else !isMatching) (
|
2022-10-25 23:35:29 +03:00
|
|
|
(map (service: { name = service; value = true; }) (builtins.attrNames services))
|
2022-09-05 19:12:09 +03:00
|
|
|
)
|
2022-12-18 15:13:45 +03:00
|
|
|
) systemdServices.definitionsWithLocations));
|
2022-09-05 19:12:09 +03:00
|
|
|
in
|
2022-10-22 20:37:44 +03:00
|
|
|
# Calculate set difference: matchingServices - nonMatchingServices
|
2022-10-25 23:35:30 +03:00
|
|
|
# and exclude unavailable services (defined via `mkIf false ...`) by checking `configSystemdServices`.
|
2022-10-22 20:37:44 +03:00
|
|
|
builtins.filter (prefixedService:
|
|
|
|
configSystemdServices ? ${prefixedService} && (! nonMatchingServices ? ${prefixedService})
|
|
|
|
) (builtins.attrNames matchingServices);
|
2022-09-05 19:12:09 +03:00
|
|
|
|
2022-10-22 20:37:44 +03:00
|
|
|
# The concatenated list of values of ExecStart, ExecStop, ... (`scriptAttrs`) of all `servicesToCheck`.
|
2022-09-05 19:12:09 +03:00
|
|
|
serviceCmds = let
|
|
|
|
scriptAttrs = [
|
|
|
|
"ExecStartPre"
|
|
|
|
"ExecStart"
|
|
|
|
"ExecStartPost"
|
|
|
|
"ExecStop"
|
|
|
|
"ExecStopPost"
|
|
|
|
"ExecCondition"
|
|
|
|
"ExecReload"
|
|
|
|
];
|
|
|
|
services = config.systemd.services;
|
|
|
|
in
|
|
|
|
builtins.concatMap (serviceName: let
|
|
|
|
serviceCfg = services.${serviceName}.serviceConfig;
|
|
|
|
in
|
|
|
|
builtins.concatMap (attr:
|
|
|
|
lib.optionals (serviceCfg ? ${attr}) (
|
|
|
|
let
|
|
|
|
cmd = serviceCfg.${attr};
|
|
|
|
in
|
|
|
|
if builtins.typeOf cmd == "list" then cmd else [ cmd ]
|
|
|
|
)
|
|
|
|
) scriptAttrs
|
2022-10-22 20:37:44 +03:00
|
|
|
) servicesToCheck;
|
2022-09-05 19:12:09 +03:00
|
|
|
|
|
|
|
# A list of all binaries included in `serviceCmds`
|
|
|
|
serviceBinaries = map (cmd: builtins.head (
|
|
|
|
# Extract the first component (the binary).
|
|
|
|
# cmd can start with extra modifiers like `+`
|
|
|
|
builtins.match "[^/]*([^[:space:]]+).*" (toString cmd)
|
|
|
|
)) serviceCmds;
|
|
|
|
|
|
|
|
shellcheckServices = pkgs.runCommand "shellcheck-services" {
|
|
|
|
inherit serviceBinaries;
|
|
|
|
# The `builtins.match` in `serviceBinaries` discards the string context, so we
|
|
|
|
# also have to add `serviceCmds` to the derivation. This ensures that all
|
|
|
|
# referenced nix paths are available to the builder.
|
|
|
|
inherit serviceCmds;
|
|
|
|
} ''
|
|
|
|
echo "Checked binaries:"
|
|
|
|
# Find and check all binaries that have a bash shebang
|
|
|
|
grep -Pl '\A#! *\S+bash\b' $serviceBinaries | while IFS= read -r script; do
|
|
|
|
echo "$script"
|
|
|
|
${pkgs.shellcheck}/bin/shellcheck --shell bash "$script"
|
|
|
|
done | tee "$out"
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit options;
|
2022-10-22 20:37:44 +03:00
|
|
|
|
|
|
|
config = mkIf (cfg.enable && cfg.sourcePrefix != null) {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = builtins.length servicesToCheck > 0;
|
|
|
|
message = "test.shellcheckServices: No services found with source prefix `${cfg.sourcePrefix}`";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
system.extraDependencies = [ shellcheckServices ];
|
|
|
|
};
|
2022-09-05 19:12:09 +03:00
|
|
|
}
|