Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] nix.nix: split, align and reuse macros from NixOS #329

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
4 changes: 2 additions & 2 deletions docs/default.nix
Original file line number Diff line number Diff line change
@@ -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; };
Expand All @@ -14,7 +14,7 @@ let
};

modules = import ../modules/module-list.nix {
inherit pkgs;
inherit pkgs nixpkgs;
home-manager-path = home-manager.outPath;
isFlake = true;
};
Expand Down
7 changes: 4 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];

Expand Down Expand Up @@ -80,6 +80,7 @@
{ modules ? [ ]
, extraSpecialArgs ? { }
, pkgs ? pkgs'
, nixpkgs ? inputs.nixpkgs
, home-manager-path ? home-manager.outPath
# deprecated:
, config ? null
Expand All @@ -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;
});
Expand All @@ -118,7 +119,7 @@
};

docs = import ./docs {
inherit home-manager;
inherit nixpkgs home-manager;
pkgs = nixpkgs.legacyPackages.${system};
nmdSrc = nmd;
};
Expand Down
3 changes: 2 additions & 1 deletion modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{ config ? null
, extraSpecialArgs ? { }
, pkgs ? import <nixpkgs> { }
, nixpkgs ? <nixpkgs>
, home-manager-path ? <home-manager>
, isFlake ? false
}:
Expand All @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions modules/environment/nix-channel.nix
Original file line number Diff line number Diff line change
@@ -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. <literal>&lt;nixpkgs&gt;</literal>).
'';
};
};

};


###### implementation

config = {
environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath;
};

}
21 changes: 21 additions & 0 deletions modules/environment/nix-flakes.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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, nixpkgs, ... }:

with lib;

let
cfg = config.nix;
in

{
imports = [
# Use options and config from upstream nix-flakes.nix
"${nixpkgs}/nixos/modules/config/nix-flakes.nix"
];
}
168 changes: 39 additions & 129 deletions modules/environment/nix.nix
Original file line number Diff line number Diff line change
@@ -1,171 +1,81 @@
# 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)

{ 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")
(renameNixOpt "extraConfig" "extraOptions")
# 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

options = {

nix = {
package = mkOption {
type = types.package;
default = pkgs.nix;
defaultText = literalExpression "pkgs.nix";
description = ''
This option specifies the Nix package instance to use throughout the system.
'';
};

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. <literal>&lt;nixpkgs&gt;</literal>).
'';
};
## From nix-daemon.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 <option>from</option> is rewritten to.";
};
flake = mkOption {
type = types.nullOr types.attrs;
default = null;
example = literalExpression "nixpkgs";
description = ''
The flake input <option>from</option> is rewritten to.
'';
};
exact = mkOption {
type = types.bool;
default = true;
description = ''
Whether the <option>from</option> reference needs to match exactly. If set,
a <option>from</option> reference like <literal>nixpkgs</literal> does not
match with a reference like <literal>nixpkgs/nixos-20.03</literal>.
'';
};
};
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.";
};

substituters = mkOption {
type = types.listOf types.str;
default = [ ];
enable = mkOption {
ShamrockLee marked this conversation as resolved.
Show resolved Hide resolved
type = types.bool;
default = true;
description = ''
A list of URLs of substituters. The official NixOS and Nix-on-Droid
substituters are added by default.
Whether to enable Nix.
Disabling Nix is not supported in NixOnDroid. This option is here to
make it compatible to the upstream NixOS modules.
'';
};

trustedPublicKeys = mkOption {
type = types.listOf types.str;
default = [ ];
package = mkOption {
type = types.package;
default = pkgs.nix;
defaultText = literalExpression "pkgs.nix";
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.
This option specifies the Nix package instance to use throughout the system.
'';
};

extraOptions = mkOption {
type = types.lines;
default = "";
description = "Extra config to be appended to <filename>/etc/nix/nix.conf</filename>.";
};
};

};


###### 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;
};
};

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;
})
];
config = {
nix = {
enable = true;
ShamrockLee marked this conversation as resolved.
Show resolved Hide resolved
settings.substituters = [
"https://cache.nixos.org"
"https://nix-on-droid.cachix.org"
];
settings.trusted-public-keys = [
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
"nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU="
];
};
};

}
Loading