2022-10-25 13:35:29 -07:00
|
|
|
{ config, pkgs, lib, extendModules, ... }@args:
|
2022-09-05 09:12:09 -07:00
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options = {
|
|
|
|
test.shellcheckServices = mkOption {
|
|
|
|
readOnly = true;
|
|
|
|
description = ''
|
|
|
|
A derivation that runs shellcheck on all bash scripts included
|
|
|
|
in nix-bitcoin services.
|
|
|
|
'';
|
|
|
|
default = shellcheckServices;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# A list of all service names that are defined by nix-bitcoin.
|
|
|
|
# [ "bitcoind", "clightning", ... ]
|
|
|
|
#
|
2022-10-25 13:35:29 -07:00
|
|
|
# Algorithm: Parse defintions of `systemd.services` and return all services
|
|
|
|
# that only have definitions located in the nix-bitcoin source.
|
2022-09-05 09:12:09 -07:00
|
|
|
nix-bitcoin-services = let
|
2022-10-25 13:35:29 -07:00
|
|
|
systemdServices = args.options.systemd.services;
|
2022-09-05 09:12:09 -07:00
|
|
|
nix-bitcoin-source = toString ../..;
|
|
|
|
nbServices = collectServices true;
|
|
|
|
nonNbServices = collectServices false;
|
|
|
|
# Return set of services ({ service1 = true; service2 = true; ... })
|
|
|
|
# which are either defined or not defined by nix-bitcoin, depending
|
|
|
|
# on `fromNixBitcoin`.
|
2022-10-25 13:35:29 -07:00
|
|
|
collectServices = fromNixBitcoin: lib.listToAttrs (builtins.concatLists (zipListsWith (services: file:
|
2022-09-05 09:12:09 -07:00
|
|
|
let
|
2022-10-25 13:35:29 -07:00
|
|
|
isNbSource = lib.hasPrefix nix-bitcoin-source file;
|
2022-09-05 09:12:09 -07:00
|
|
|
in
|
2022-10-25 13:35:29 -07:00
|
|
|
# Nix has no boolean XOR, so use `if`
|
2022-09-05 09:12:09 -07:00
|
|
|
lib.optionals (if fromNixBitcoin then isNbSource else !isNbSource) (
|
2022-10-25 13:35:29 -07:00
|
|
|
(map (service: { name = service; value = true; }) (builtins.attrNames services))
|
2022-09-05 09:12:09 -07:00
|
|
|
)
|
2022-10-25 13:35:29 -07:00
|
|
|
# TODO-EXTERNAL:
|
|
|
|
# Use `systemdServices.definitionsWithLocations` when https://github.com/NixOS/nixpkgs/pull/189836
|
|
|
|
# is included in nixpkgs stable.
|
|
|
|
) systemdServices.definitions systemdServices.files));
|
2022-09-05 09:12:09 -07:00
|
|
|
in
|
|
|
|
# Set difference: nbServices - nonNbServices
|
|
|
|
builtins.filter (nbService: ! nonNbServices ? ${nbService}) (builtins.attrNames nbServices);
|
|
|
|
|
|
|
|
# The concatenated list of values of ExecStart, ExecStop, ... (`scriptAttrs`) of all `nix-bitcoin-services`.
|
|
|
|
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
|
|
|
|
) nix-bitcoin-services;
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
}
|