From dd618800566a51365411683f9a4c789923cdc874 Mon Sep 17 00:00:00 2001 From: Seth Flynn Date: Mon, 10 Feb 2025 15:58:39 -0500 Subject: flake: use checks for CI --- lib/default.nix | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lib/default.nix (limited to 'lib') diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..0d36185 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,80 @@ +{ config, lib, ... }: + +let + self = config.flake.lib; +in + +{ + + flake.lib = { + /** + Get the derivation attribute of a configuration if needed + + # Type + + ``` + derivationFrom :: AttrSet -> Derivation + ``` + + # Arguments + + - [set] A system/home configuration or regular derivation + */ + derivationFrom = + deriv: + if lib.isDerivation deriv then + deriv + else + deriv.config.system.build.toplevel or deriv.activationPackage; + + /** + Check if a derivation or configuration is compatible with the current system + + # Type + + ``` + isCompatible :: String -> Derivation -> Bool + ``` + + # Arguments + + - [system] System to check against + - [derivation] Derivation to check + */ + isCompatibleWith = system: deriv: (deriv.pkgs or deriv).stdenv.hostPlatform.system == system; + + /** + Flatten nested derivations from an attribute set + + Mainly for use with making Flake outputs work in `checks` + + # Example + + ```nix + collectNestedDerivations { nixosConfigurations = { my-machine = { }; }; } + => { nixosConfigurations-my-machine = { }; } + + # Type + + ``` + collectNestedDerivations :: String -> AttrSet -> AttrSet + ``` + + # Arguments + + - [system] System to collect derivations for + - [set] Set of (single-level) nested derivations + */ + collectNestedDerivationsFor = + system: + + lib.foldlAttrs ( + acc: attrType: values: + + acc + // lib.mapAttrs' ( + attrName: value: lib.nameValuePair "${attrType}-${attrName}" (self.derivationFrom value) + ) (lib.filterAttrs (lib.const (self.isCompatibleWith system)) values) + ) { }; + }; +} -- cgit v1.2.3