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
22 flakeRefFormat = ''
23 The format of flake references is described in {manpage}`nix3-flake(1)`.
24 '';
25
26in
27{
28 options = {
29 nix = {
30 registry = mkOption {
31 type = types.attrsOf (
32 types.submodule (
33 let
34 referenceAttrs =
35 with types;
36 attrsOf (oneOf [
37 str
38 int
39 bool
40 path
41 package
42 ]);
43 in
44 { config, name, ... }:
45 {
46 options = {
47 from = mkOption {
48 type = referenceAttrs;
49 example = {
50 type = "indirect";
51 id = "nixpkgs";
52 };
53 description = ''
54 The flake reference to be rewritten.
55
56 ${flakeRefFormat}
57 '';
58 };
59 to = mkOption {
60 type = referenceAttrs;
61 example = {
62 type = "github";
63 owner = "my-org";
64 repo = "my-nixpkgs";
65 };
66 description = ''
67 The flake reference {option}`from` is rewritten to.
68
69 ${flakeRefFormat}
70 '';
71 };
72 flake = mkOption {
73 type = types.nullOr types.attrs;
74 default = null;
75 example = literalExpression "nixpkgs";
76 description = ''
77 The flake input {option}`from` is rewritten to.
78 '';
79 };
80 exact = mkOption {
81 type = types.bool;
82 default = true;
83 description = ''
84 Whether the {option}`from` reference needs to match exactly. If set,
85 a {option}`from` reference like `nixpkgs` does not
86 match with a reference like `nixpkgs/nixos-20.03`.
87 '';
88 };
89 };
90 config = {
91 from = mkDefault {
92 type = "indirect";
93 id = name;
94 };
95 to = mkIf (config.flake != null) (
96 mkDefault (
97 {
98 type = "path";
99 path = config.flake.outPath;
100 }
101 // filterAttrs (n: _: n == "lastModified" || n == "rev" || n == "narHash") config.flake
102 )
103 );
104 };
105 }
106 )
107 );
108 default = { };
109 description = ''
110 A system-wide flake registry.
111
112 See {manpage}`nix3-registry(1)` for more information.
113 '';
114 };
115 };
116 };
117
118 config = mkIf cfg.enable {
119 environment.etc."nix/registry.json".text = builtins.toJSON {
120 version = 2;
121 flakes = mapAttrsToList (n: v: { inherit (v) from to exact; }) cfg.registry;
122 };
123 };
124}