summaryrefslogtreecommitdiff
path: root/lib/default.nix
blob: 0d36185dd649d337e8db7f00ece77cc1d9251600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)
      ) { };
  };
}