summaryrefslogtreecommitdiff
path: root/lib/ci.nix
blob: 36185f652d71ef8e2ab00cc0069a9f64402cd893 (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
lib: supportedSystems: let
  inherit (builtins) attrNames baseNameOf elem getContext head mapAttrs seq stringLength substring;
  inherit (lib) filterAttrs;
  check = string: elem string supportedSystems;
in rec {
  # filters systems in basic flake output
  # ex:
  #
  #	packages = {
  #	  x86_64-linux = {};
  #	  aarch64-linux = {};
  #	  x86_64-darwin = {};
  #	  aarch64-darwin = {};
  #	};
  # mkCompatible packages -> {x86_64-linux = {}; aarch64-linux = {};}
  mkCompatible = filterAttrs (system: _: check system);

  # mkCompatible but for apps, since their attribute
  # also needs to be editied in order to be picked up
  # by hydra
  mkCompatibleApps = apps:
    mkCompatible (mapAttrs (
        _:
          mapAttrs (_: v: {
            program = let
              ctx = getContext v.program;
              drvPath = head (attrNames ctx);
              basename = baseNameOf drvPath;
              hashLength = 33;
              l = stringLength basename;
            in {
              name = substring hashLength (l - hashLength - 4) basename;
              type = "derivation";
              inherit drvPath;
            };
          })
      )
      apps);

  # mkCompatible but for formatters
  mkCompatibleFormatters = filterAttrs (system: fmt: check system && elem system (fmt.meta.platforms or []));

  # mkComaptible, but maps nixosConfigurations
  # to their toplevel build attribute so they can
  # be picked up by hydra
  mkCompatibleCfg = configs:
    filterAttrs (_: config: check config.system)
    (mapAttrs (_: v: v.config.system.build.toplevel) configs);

  # mkCompatibleCfg, but the toplevel build attribute
  # is only evaluated
  mkCompatibleCfg' = configs:
    filterAttrs (_: config: check config.system)
    (mapAttrs (_: v:
      seq
      v.config.system.build.toplevel
      v._module.args.pkgs.emptyFile)
    configs);

  # mkCompatible, but maps homeConfigurations
  # to their activationPackage so they can be
  # picked up by hydra
  mkCompatibleHM = configs:
    filterAttrs (system: _: check system)
    (mapAttrs (_: mapAttrs (_: deriv: deriv.activationPackage or {})) configs);

  # mkCompatible, but for packages
  # meta.platforms is also checked to ensure compatibility
  mkCompatiblePkgs = mapAttrs (system: filterAttrs (_: deriv: elem system (deriv.meta.platforms or [])));
}