From e22369c17bb6865efe9beca930c95c34e40ace06 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Sun, 17 Dec 2023 23:22:22 +0100 Subject: [PATCH] nix: introduce nix.registry options Signed-off-by: Sefa Eyeoglu --- modules/module-list.nix | 1 + modules/nix/flakes.nix | 97 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 modules/nix/flakes.nix diff --git a/modules/module-list.nix b/modules/module-list.nix index 93c63bf81..f15957d26 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -40,6 +40,7 @@ ./time ./networking ./nix + ./nix/flakes.nix ./nix/linux-builder.nix ./nix/nix-darwin.nix ./nix/nixpkgs.nix diff --git a/modules/nix/flakes.nix b/modules/nix/flakes.nix new file mode 100644 index 000000000..07a935480 --- /dev/null +++ b/modules/nix/flakes.nix @@ -0,0 +1,97 @@ +/* + Manages the flake registry. + + Based on /nixos/modules/config/nix-flakes.nix + */ +{ config, lib, ... }: +let + inherit (lib) + filterAttrs + literalExpression + mapAttrsToList + mkDefault + mkIf + mkOption + types + ; + + cfg = config.nix; + +in +{ + meta.maintainers = with lib.maintainers; [ + Scrumplex + ]; + + options = { + nix = { + registry = mkOption { + type = types.attrsOf (types.submodule ( + let + referenceAttrs = with types; attrsOf (oneOf [ + str + int + bool + path + package + ]); + in + { config, name, ... }: + { + options = { + from = mkOption { + type = referenceAttrs; + example = { type = "indirect"; id = "nixpkgs"; }; + description = lib.mdDoc "The flake reference to be rewritten."; + }; + to = mkOption { + type = referenceAttrs; + example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; }; + description = lib.mdDoc "The flake reference {option}`from` is rewritten to."; + }; + flake = mkOption { + type = types.nullOr types.attrs; + default = null; + example = literalExpression "nixpkgs"; + description = lib.mdDoc '' + The flake input {option}`from` is rewritten to. + ''; + }; + exact = mkOption { + type = types.bool; + default = true; + description = lib.mdDoc '' + Whether the {option}`from` reference needs to match exactly. If set, + a {option}`from` reference like `nixpkgs` does not + match with a reference like `nixpkgs/nixos-20.03`. + ''; + }; + }; + config = { + from = mkDefault { type = "indirect"; id = name; }; + to = mkIf (config.flake != null) (mkDefault ( + { + type = "path"; + path = config.flake.outPath; + } // filterAttrs + (n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash") + config.flake + )); + }; + } + )); + default = { }; + description = lib.mdDoc '' + A system-wide flake registry. + ''; + }; + }; + }; + + config = mkIf cfg.enable { + environment.etc."nix/registry.json".text = builtins.toJSON { + version = 2; + flakes = mapAttrsToList (n: v: { inherit (v) from to exact; }) cfg.registry; + }; + }; +}