From aa6261bb96b8e19805d6b7ea764929f015a0ac09 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Fri, 28 Jan 2022 04:58:25 +1100 Subject: [PATCH] nix: add module (#2623) --- modules/misc/nix.nix | 86 ++++++++++++++++++++++++++++++++++++++++++++ modules/modules.nix | 1 + 2 files changed, 87 insertions(+) create mode 100644 modules/misc/nix.nix diff --git a/modules/misc/nix.nix b/modules/misc/nix.nix new file mode 100644 index 000000000..a044400fa --- /dev/null +++ b/modules/misc/nix.nix @@ -0,0 +1,86 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.nix; + +in { + options.nix = { + registry = mkOption { + type = types.attrsOf (types.submodule (let + inputAttrs = types.attrsOf + (types.oneOf [ types.str types.int types.bool types.package ]); + in { config, name, ... }: { + options = { + from = mkOption { + type = inputAttrs; + example = { + type = "indirect"; + id = "nixpkgs"; + }; + description = "The flake reference to be rewritten."; + }; + to = mkOption { + type = inputAttrs; + example = { + type = "github"; + owner = "my-org"; + repo = "my-nixpkgs"; + }; + description = + "The flake reference to which is to be rewritten."; + }; + flake = mkOption { + type = types.nullOr types.attrs; + default = null; + example = literalExpression "nixpkgs"; + description = '' + The flake input to which is to be rewritten. + ''; + }; + exact = mkOption { + type = types.bool; + default = true; + description = '' + Whether the reference needs to match exactly. If set, + a 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) ({ + type = "path"; + path = config.flake.outPath; + } // lib.filterAttrs (n: v: + n == "lastModified" || n == "rev" || n == "revCount" || n + == "narHash") config.flake); + }; + })); + default = { }; + description = '' + User level flake registry. + ''; + }; + registryVersion = mkOption { + type = types.int; + default = 2; + internal = true; + description = "The flake registry format version."; + }; + }; + + config = mkIf (cfg.registry != { }) { + xdg.configFile."nix/registry.json".text = builtins.toJSON { + version = cfg.registryVersion; + flakes = + mapAttrsToList (n: v: { inherit (v) from to exact; }) cfg.registry; + }; + }; +} diff --git a/modules/modules.nix b/modules/modules.nix index 844a62ad7..dc5921fed 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -257,6 +257,7 @@ let ./xcursor.nix ./xresources.nix ./xsession.nix + ./misc/nix.nix (pkgs.path + "/nixos/modules/misc/assertions.nix") (pkgs.path + "/nixos/modules/misc/meta.nix") ] ++ optional useNixpkgsModule ./misc/nixpkgs.nix