summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix80
1 files changed, 80 insertions, 0 deletions
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)
+ ) { };
+ };
+}