From a413124033a80507a4f915391de7bef740d78d36 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Fri, 16 Feb 2024 00:49:44 +0800 Subject: [PATCH 1/7] Remove outdated compatibility-with-NixOS option alias In NixOS 23.11, `option.nix.extraConfig` was already renamed into `option.nix.extraOption`, and no alias was left. We no longer need the alias `option.nix.extraConfig` anymore. --- modules/environment/nix.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/environment/nix.nix b/modules/environment/nix.nix index c5295a91..2f019718 100644 --- a/modules/environment/nix.nix +++ b/modules/environment/nix.nix @@ -20,7 +20,6 @@ in imports = [ (renameNixOpt "binaryCaches" "substituters") (renameNixOpt "binaryCachePublicKeys" "trustedPublicKeys") - (renameNixOpt "extraConfig" "extraOptions") ]; ###### interface From 2c7f03a93e979c4b3a6561bfe0dcd98d8a9f3e2d Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Fri, 16 Feb 2024 03:36:52 +0800 Subject: [PATCH 2/7] nix.nix: do not unset NIX_PATH even when empty Align this behavior with Nixpkgs/NixOS. --- modules/environment/nix.nix | 54 +++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/modules/environment/nix.nix b/modules/environment/nix.nix index 2f019718..75e64ab6 100644 --- a/modules/environment/nix.nix +++ b/modules/environment/nix.nix @@ -134,37 +134,33 @@ in ###### implementation - config = mkMerge [ - { - environment.etc = { - "nix/nix.conf".text = '' - sandbox = false - substituters = ${concatStringsSep " " cfg.substituters} - trusted-public-keys = ${concatStringsSep " " cfg.trustedPublicKeys} - ${cfg.extraOptions} - ''; - - "nix/registry.json".text = builtins.toJSON { - version = 2; - flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry; - }; + config = { + environment.etc = { + "nix/nix.conf".text = '' + sandbox = false + substituters = ${concatStringsSep " " cfg.substituters} + trusted-public-keys = ${concatStringsSep " " cfg.trustedPublicKeys} + ${cfg.extraOptions} + ''; + + "nix/registry.json".text = builtins.toJSON { + version = 2; + flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry; }; + }; - nix = { - substituters = [ - "https://cache.nixos.org" - "https://nix-on-droid.cachix.org" - ]; - trustedPublicKeys = [ - "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" - "nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU=" - ]; - }; - } + nix = { + substituters = [ + "https://cache.nixos.org" + "https://nix-on-droid.cachix.org" + ]; + trustedPublicKeys = [ + "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" + "nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU=" + ]; + }; - (mkIf (cfg.nixPath != [ ]) { - environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath; - }) - ]; + environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath; + }; } From cee3b09b6c79587eb07e0846571d2b27b9cbc42d Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Fri, 16 Feb 2024 02:06:14 +0800 Subject: [PATCH 3/7] nix.nix: add option nix.enable for compatibility Add `option.nix.enable` to ease integration with NixOS macros. Hard-code `{ config.nix.enable = true; }`. --- modules/environment/nix.nix | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/environment/nix.nix b/modules/environment/nix.nix index 75e64ab6..04850290 100644 --- a/modules/environment/nix.nix +++ b/modules/environment/nix.nix @@ -27,6 +27,16 @@ in options = { nix = { + enable = mkOption { + type = types.bool; + default = true; + description = '' + Whether to enable Nix. + Disabling Nix is not supported in NixOnDroid. This option is here to + make it compatible to the upstream NixOS modules. + ''; + }; + package = mkOption { type = types.package; default = pkgs.nix; @@ -150,6 +160,7 @@ in }; nix = { + enable = true; substituters = [ "https://cache.nixos.org" "https://nix-on-droid.cachix.org" From 23b8cce084ff47723f2c806b3e6b0eac32dd89bf Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Fri, 16 Feb 2024 01:15:04 +0800 Subject: [PATCH 4/7] nix-flakes.nix, nix-channel.nix: split out from nix.nix Decouple the modules, enable ones to import/disable them separately, and to align with the upstream Nixpkgs/NixOS module system. --- modules/environment/nix-channel.nix | 44 ++++++++++++++ modules/environment/nix-flakes.nix | 92 +++++++++++++++++++++++++++++ modules/environment/nix.nix | 81 +++---------------------- 3 files changed, 143 insertions(+), 74 deletions(-) create mode 100644 modules/environment/nix-channel.nix create mode 100644 modules/environment/nix-flakes.nix diff --git a/modules/environment/nix-channel.nix b/modules/environment/nix-channel.nix new file mode 100644 index 00000000..deff5960 --- /dev/null +++ b/modules/environment/nix-channel.nix @@ -0,0 +1,44 @@ +# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE. + +# Based on +# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/nix-channel.nix +# (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors, +# licensed under MIT License as well) + +{ config, lib, pkgs, nixpkgs, ... }: + +with lib; + +let + cfg = config.nix; + renameNixOpt = old: new: + (mkRenamedOptionModule [ "nix" old ] [ "nix" new ]); +in + +{ + ###### interface + + options = { + + nix = { + nixPath = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + The default Nix expression search path, used by the Nix + evaluator to look up paths enclosed in angle brackets + (e.g. <nixpkgs>). + ''; + }; + }; + + }; + + + ###### implementation + + config = { + environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath; + }; + +} diff --git a/modules/environment/nix-flakes.nix b/modules/environment/nix-flakes.nix new file mode 100644 index 00000000..8021d025 --- /dev/null +++ b/modules/environment/nix-flakes.nix @@ -0,0 +1,92 @@ +# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE. + +# Based on +# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/nix-flakes.nix +# (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors, +# licensed under MIT License as well) + +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.nix; +in + +{ + ###### interface + + options = { + nix = { + registry = mkOption { + type = types.attrsOf (types.submodule ( + let + referenceAttrs = with types; attrsOf (oneOf [ + str + int + bool + package + ]); + in + { config, name, ... }: + { + options = { + from = mkOption { + type = referenceAttrs; + example = { type = "indirect"; id = "nixpkgs"; }; + description = "The flake reference to be rewritten."; + }; + to = mkOption { + type = referenceAttrs; + example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; }; + description = "The flake reference is rewritten to."; + }; + flake = mkOption { + type = types.nullOr types.attrs; + default = null; + example = literalExpression "nixpkgs"; + description = '' + The flake input is rewritten to. + ''; + }; + 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) (mkDefault + { + type = "path"; + path = config.flake.outPath; + } // filterAttrs + (n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash") + config.flake); + }; + } + )); + default = { }; + description = "A system-wide flake registry."; + }; + }; + }; + + + ###### implementation + + config = { + environment.etc = { + "nix/registry.json".text = builtins.toJSON { + version = 2; + flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry; + }; + }; + }; + +} diff --git a/modules/environment/nix.nix b/modules/environment/nix.nix index 04850290..0b58c63b 100644 --- a/modules/environment/nix.nix +++ b/modules/environment/nix.nix @@ -1,7 +1,9 @@ # Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE. # Based on -# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/nix-daemon.nix +# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/system/nix-daemon.nix +# and +# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/nix.nix # (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors, # licensed under MIT License as well) @@ -27,6 +29,9 @@ in options = { nix = { + + ## From nix-daemon.nix + enable = mkOption { type = types.bool; default = true; @@ -46,72 +51,7 @@ in ''; }; - nixPath = mkOption { - type = types.listOf types.str; - default = [ ]; - description = '' - The default Nix expression search path, used by the Nix - evaluator to look up paths enclosed in angle brackets - (e.g. <nixpkgs>). - ''; - }; - - registry = mkOption { - type = types.attrsOf (types.submodule ( - let - referenceAttrs = with types; attrsOf (oneOf [ - str - int - bool - package - ]); - in - { config, name, ... }: - { - options = { - from = mkOption { - type = referenceAttrs; - example = { type = "indirect"; id = "nixpkgs"; }; - description = "The flake reference to be rewritten."; - }; - to = mkOption { - type = referenceAttrs; - example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; }; - description = "The flake reference is rewritten to."; - }; - flake = mkOption { - type = types.nullOr types.attrs; - default = null; - example = literalExpression "nixpkgs"; - description = '' - The flake input is rewritten to. - ''; - }; - 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) (mkDefault - { - type = "path"; - path = config.flake.outPath; - } // filterAttrs - (n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash") - config.flake); - }; - } - )); - default = { }; - description = "A system-wide flake registry."; - }; + ## From nix.nix substituters = mkOption { type = types.listOf types.str; @@ -152,11 +92,6 @@ in trusted-public-keys = ${concatStringsSep " " cfg.trustedPublicKeys} ${cfg.extraOptions} ''; - - "nix/registry.json".text = builtins.toJSON { - version = 2; - flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry; - }; }; nix = { @@ -170,8 +105,6 @@ in "nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU=" ]; }; - - environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath; }; } From 5828992466d2f739c663c9f5cd38c98e0c299b1b Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Fri, 16 Feb 2024 01:03:01 +0800 Subject: [PATCH 5/7] modules: inject nixpkgs source as a special arg --- docs/default.nix | 4 ++-- flake.nix | 7 ++++--- modules/default.nix | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/default.nix b/docs/default.nix index 3df0cf60..d64a8d5f 100644 --- a/docs/default.nix +++ b/docs/default.nix @@ -1,6 +1,6 @@ # Copyright (c) 2019-2023, see AUTHORS. Licensed under MIT License, see LICENSE. -{ pkgs, home-manager, nmdSrc }: +{ pkgs, nixpkgs, home-manager, nmdSrc }: let nmd = import nmdSrc { inherit pkgs; }; @@ -14,7 +14,7 @@ let }; modules = import ../modules/module-list.nix { - inherit pkgs; + inherit pkgs nixpkgs; home-manager-path = home-manager.outPath; isFlake = true; }; diff --git a/flake.nix b/flake.nix index 539f89d1..8ee10eb5 100644 --- a/flake.nix +++ b/flake.nix @@ -25,7 +25,7 @@ }; }; - outputs = { self, nixpkgs, nixpkgs-for-bootstrap, home-manager, nix-formatter-pack, nmd }: + outputs = { self, nixpkgs, nixpkgs-for-bootstrap, home-manager, nix-formatter-pack, nmd }@inputs: let forEachSystem = nixpkgs.lib.genAttrs [ "aarch64-linux" "x86_64-linux" ]; @@ -80,6 +80,7 @@ { modules ? [ ] , extraSpecialArgs ? { } , pkgs ? pkgs' + , nixpkgs ? inputs.nixpkgs , home-manager-path ? home-manager.outPath # deprecated: , config ? null @@ -103,7 +104,7 @@ See the 22.11 release notes for more. '' (import ./modules { - inherit extraSpecialArgs home-manager-path pkgs; + inherit extraSpecialArgs home-manager-path nixpkgs pkgs; config.imports = modules; isFlake = true; }); @@ -118,7 +119,7 @@ }; docs = import ./docs { - inherit home-manager; + inherit nixpkgs home-manager; pkgs = nixpkgs.legacyPackages.${system}; nmdSrc = nmd; }; diff --git a/modules/default.nix b/modules/default.nix index 0ad1872a..6412b0a1 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -3,6 +3,7 @@ { config ? null , extraSpecialArgs ? { } , pkgs ? import { } +, nixpkgs ? , home-manager-path ? , isFlake ? false }: @@ -21,7 +22,7 @@ let rawModule = evalModules { modules = [ configModule ] ++ nodModules; - specialArgs = extraSpecialArgs; + specialArgs = { inherit nixpkgs; } // extraSpecialArgs; }; failedAssertions = map (x: x.message) (filter (x: !x.assertion) rawModule.config.assertions); From 0f78892de8a3f8c7bfc8fdea94bdfb3c3abea343 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Fri, 16 Feb 2024 01:58:46 +0800 Subject: [PATCH 6/7] nix.nix: inherit from upstream nix.nix --- modules/environment/nix.nix | 55 +++++++++---------------------------- 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/modules/environment/nix.nix b/modules/environment/nix.nix index 0b58c63b..c1995500 100644 --- a/modules/environment/nix.nix +++ b/modules/environment/nix.nix @@ -7,21 +7,27 @@ # (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors, # licensed under MIT License as well) -{ config, lib, pkgs, ... }: +{ config, lib, pkgs, nixpkgs, ... }: with lib; let cfg = config.nix; renameNixOpt = old: new: - (mkRenamedOptionModule [ "nix" old ] [ "nix" new ]); + mkRenamedOptionModuleWith { + sinceRelease = 2205; + from = [ "nix" old ]; + to = [ "nix" "settings" new ]; + }; in { - # Backward-compatibility with the NixOS options. imports = [ - (renameNixOpt "binaryCaches" "substituters") - (renameNixOpt "binaryCachePublicKeys" "trustedPublicKeys") + # Use options and config from upstream nix.nix + "${nixpkgs}/nixos/modules/config/nix.nix" + # Backward-compatibility with pre-`settings` options. + (renameNixOpt "substituters" "substituters") + (renameNixOpt "trustedPublicKeys" "trusted-public-keys") ]; ###### interface @@ -51,32 +57,6 @@ in ''; }; - ## From nix.nix - - substituters = mkOption { - type = types.listOf types.str; - default = [ ]; - description = '' - A list of URLs of substituters. The official NixOS and Nix-on-Droid - substituters are added by default. - ''; - }; - - trustedPublicKeys = mkOption { - type = types.listOf types.str; - default = [ ]; - description = '' - A list of public keys. When paths are copied from another Nix store (such as a - binary cache), they must be signed with one of these keys. The official NixOS - and Nix-on-Droid public keys are added by default. - ''; - }; - - extraOptions = mkOption { - type = types.lines; - default = ""; - description = "Extra config to be appended to /etc/nix/nix.conf."; - }; }; }; @@ -85,22 +65,13 @@ in ###### implementation config = { - environment.etc = { - "nix/nix.conf".text = '' - sandbox = false - substituters = ${concatStringsSep " " cfg.substituters} - trusted-public-keys = ${concatStringsSep " " cfg.trustedPublicKeys} - ${cfg.extraOptions} - ''; - }; - nix = { enable = true; - substituters = [ + settings.substituters = [ "https://cache.nixos.org" "https://nix-on-droid.cachix.org" ]; - trustedPublicKeys = [ + settings.trusted-public-keys = [ "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" "nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU=" ]; From 357af61846ea4d059aff97930e6c9c395b07effc Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Sun, 18 Feb 2024 09:33:11 +0800 Subject: [PATCH 7/7] nix-flakes.nix: inherit from upstream nix-flakes.nix --- modules/environment/nix-flakes.nix | 81 ++---------------------------- 1 file changed, 5 insertions(+), 76 deletions(-) diff --git a/modules/environment/nix-flakes.nix b/modules/environment/nix-flakes.nix index 8021d025..e3063b6e 100644 --- a/modules/environment/nix-flakes.nix +++ b/modules/environment/nix-flakes.nix @@ -5,7 +5,7 @@ # (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors, # licensed under MIT License as well) -{ config, lib, pkgs, ... }: +{ config, lib, pkgs, nixpkgs, ... }: with lib; @@ -14,79 +14,8 @@ let in { - ###### interface - - options = { - nix = { - registry = mkOption { - type = types.attrsOf (types.submodule ( - let - referenceAttrs = with types; attrsOf (oneOf [ - str - int - bool - package - ]); - in - { config, name, ... }: - { - options = { - from = mkOption { - type = referenceAttrs; - example = { type = "indirect"; id = "nixpkgs"; }; - description = "The flake reference to be rewritten."; - }; - to = mkOption { - type = referenceAttrs; - example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; }; - description = "The flake reference is rewritten to."; - }; - flake = mkOption { - type = types.nullOr types.attrs; - default = null; - example = literalExpression "nixpkgs"; - description = '' - The flake input is rewritten to. - ''; - }; - 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) (mkDefault - { - type = "path"; - path = config.flake.outPath; - } // filterAttrs - (n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash") - config.flake); - }; - } - )); - default = { }; - description = "A system-wide flake registry."; - }; - }; - }; - - - ###### implementation - - config = { - environment.etc = { - "nix/registry.json".text = builtins.toJSON { - version = 2; - flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry; - }; - }; - }; - + imports = [ + # Use options and config from upstream nix-flakes.nix + "${nixpkgs}/nixos/modules/config/nix-flakes.nix" + ]; }