test/shellcheck-services: simplify accessing service definitions

This also improves performance by removing the extra module evaluation.
This commit is contained in:
Erik Arvstedt 2022-10-25 22:35:29 +02:00
parent 9d074e1985
commit 46f17fe313
No known key found for this signature in database
GPG Key ID: 33312B944DD97846

View File

@ -1,4 +1,4 @@
{ config, pkgs, lib, extendModules, ... }: { config, pkgs, lib, extendModules, ... }@args:
with lib; with lib;
let let
options = { options = {
@ -12,65 +12,31 @@ let
}; };
}; };
# TODO-EXTERNAL:
# This can be removed when https://github.com/NixOS/nixpkgs/pull/189836 is merged.
#
# A list of all systemd service definitions and their locations, with format
# [
# {
# file = ...;
# value = { postgresql = ...; };
# }
# ...
# ]
systemdServiceDefs =
(extendModules {
modules = [
{
# Currently, NixOS modules only allow accessing option definition locations
# via type.merge.
# Override option `systemd.services` and use it to return the list of service defs.
options.systemd.services = lib.mkOption {
type = lib.types.anything // {
merge = loc: defs: defs;
};
};
# Disable all modules that define options.systemd.services so that these
# defs don't collide with our definition
disabledModules = [
"system/boot/systemd.nix"
# These files amend option systemd.services
"testing/service-runner.nix"
"security/systemd-confinement.nix"
];
config._module.check = false;
}
];
}).config.systemd.services;
# A list of all service names that are defined by nix-bitcoin. # A list of all service names that are defined by nix-bitcoin.
# [ "bitcoind", "clightning", ... ] # [ "bitcoind", "clightning", ... ]
# #
# Algorithm: Parse `systemdServiceDefs` and return all services that # Algorithm: Parse defintions of `systemd.services` and return all services
# only have definitions located in the nix-bitcoin source. # that only have definitions located in the nix-bitcoin source.
nix-bitcoin-services = let nix-bitcoin-services = let
systemdServices = args.options.systemd.services;
nix-bitcoin-source = toString ../..; nix-bitcoin-source = toString ../..;
nbServices = collectServices true; nbServices = collectServices true;
nonNbServices = collectServices false; nonNbServices = collectServices false;
# Return set of services ({ service1 = true; service2 = true; ... }) # Return set of services ({ service1 = true; service2 = true; ... })
# which are either defined or not defined by nix-bitcoin, depending # which are either defined or not defined by nix-bitcoin, depending
# on `fromNixBitcoin`. # on `fromNixBitcoin`.
collectServices = fromNixBitcoin: lib.listToAttrs (builtins.concatLists (map (def: collectServices = fromNixBitcoin: lib.listToAttrs (builtins.concatLists (zipListsWith (services: file:
let let
isNbSource = lib.hasPrefix nix-bitcoin-source def.file; isNbSource = lib.hasPrefix nix-bitcoin-source file;
in in
# Nix has nor boolean XOR, so use `if` # Nix has no boolean XOR, so use `if`
lib.optionals (if fromNixBitcoin then isNbSource else !isNbSource) ( lib.optionals (if fromNixBitcoin then isNbSource else !isNbSource) (
(map (service: { name = service; value = true; }) (builtins.attrNames def.value)) (map (service: { name = service; value = true; }) (builtins.attrNames services))
) )
) systemdServiceDefs)); # TODO-EXTERNAL:
# Use `systemdServices.definitionsWithLocations` when https://github.com/NixOS/nixpkgs/pull/189836
# is included in nixpkgs stable.
) systemdServices.definitions systemdServices.files));
in in
# Set difference: nbServices - nonNbServices # Set difference: nbServices - nonNbServices
builtins.filter (nbService: ! nonNbServices ? ${nbService}) (builtins.attrNames nbServices); builtins.filter (nbService: ! nonNbServices ? ${nbService}) (builtins.attrNames nbServices);