1/*
2 Manages the flake registry.
3
4 See also
5 - ./nix.nix
6 - ./nix-channel.nix
7 */
8{ config, lib, ... }:
9let
10 inherit (lib)
11 filterAttrs
12 literalExpression
13 mapAttrsToList
14 mkDefault
15 mkIf
16 mkOption
17 types
18 ;
19
20 cfg = config.nix;
21
22in
23{
24 options = {
25 nix = {
26 registry = mkOption {
27 type = types.attrsOf (types.submodule (
28 let
29 referenceAttrs = with types; attrsOf (oneOf [
30 str
31 int
32 bool
33 path
34 package
35 ]);
36 in
37 { config, name, ... }:
38 {
39 options = {
40 from = mkOption {
41 type = referenceAttrs;
42 example = { type = "indirect"; id = "nixpkgs"; };
43 description = "The flake reference to be rewritten.";
44 };
45 to = mkOption {
46 type = referenceAttrs;
47 example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; };
48 description = "The flake reference {option}`from` is rewritten to.";
49 };
50 flake = mkOption {
51 type = types.nullOr types.attrs;
52 default = null;
53 example = literalExpression "nixpkgs";
54 description = ''
55 The flake input {option}`from` is rewritten to.
56 '';
57 };
58 exact = mkOption {
59 type = types.bool;
60 default = true;
61 description = ''
62 Whether the {option}`from` reference needs to match exactly. If set,
63 a {option}`from` reference like `nixpkgs` does not
64 match with a reference like `nixpkgs/nixos-20.03`.
65 '';
66 };
67 };
68 config = {
69 from = mkDefault { type = "indirect"; id = name; };
70 to = mkIf (config.flake != null) (mkDefault (
71 {
72 type = "path";
73 path = config.flake.outPath;
74 } // filterAttrs
75 (n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash")
76 config.flake
77 ));
78 };
79 }
80 ));
81 default = { };
82 description = ''
83 A system-wide flake registry.
84 '';
85 };
86 };
87 };
88
89 config = mkIf cfg.enable {
90 environment.etc."nix/registry.json".text = builtins.toJSON {
91 version = 2;
92 flakes = mapAttrsToList (n: v: { inherit (v) from to exact; }) cfg.registry;
93 };
94 };
95}