1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-14 11:57:34 +00:00

eval-config: rationalize handling of Nixpkgs

This is a big change that disentangles a lot of mistaken assumptions
about mixing multiple versions of Nixpkgs, treating external flake
inputs as gospel for the source of Nixpkgs and nix-darwin, etc.;
the end result should be much simpler conceptually, but it will be a
breaking change for anyone using `eval-config.nix` directly. Hopefully
that shouldn't be a big issue, as it is more of an internal API and
it's quite likely that existing uses may have been broken in the same
way the internal ones were.

It was previously easy to get into a state where your `lib` comes
from nix-darwin's `nixpkgs` input or a global channel and your
`pkgs` comes from another major version of Nixpkgs. This is pretty
fundamentally broken due to the coupling of `pkgs` to its corresponding
`lib`, but the brokenness was hidden much of the time until something
surfaced it. Now there is exactly one mandatory `lib` input to system
evaluation, and the handling of various additional options like `pkgs`
and `system` can be done modularly; maintaining backwards compatibility
with the previous calling convention is punted to the `default.nix`
and `lib.darwinSystem` entry points. `inputs` is no longer read by
nix-darwin or special in any way, merely a convention for user code,
and the argument is retained in the entry points only for backwards
compatibility.

All correct invocations of the entry points should keep working
after this change, and some previously-broken ones should be fixed
too. The documentation and template have been adjusted to show the
newly-recommended modular way of specifying various things, but no
deprecation warnings have been introduced yet by this change.

There is one potential, mostly cosmetic regression:
`system.nixpkgsRevision` and related options are less likely to be
set than before, in cases where it is not possible to determine the
origin of the package set. Setting `nixpkgs.source` explicitly will
make this work again, and I hope to look into sending changes upstream
to Nixpkgs to make `lib.trivial.revisionWithDefault` behave properly
under flakes, which would fix this regression and potentially allow
reducing some of the complexity.

Fixes: #669
This commit is contained in:
Emily 2023-07-07 09:02:38 +01:00
parent 51ba5e614a
commit f9724c4543
6 changed files with 56 additions and 92 deletions

View file

@ -85,39 +85,29 @@ A minimal example of using an existing configuration.nix:
darwin.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, darwin, nixpkgs }: {
outputs = inputs@{ self, darwin, nixpkgs }: {
darwinConfigurations."Johns-MacBook" = darwin.lib.darwinSystem {
system = "x86_64-darwin";
modules = [ ./configuration.nix ];
};
};
}
```
Inputs from the flake can also be passed to `darwinSystem`. These inputs are then
Inputs from the flake can also be passed into `darwinSystem`. These inputs are then
accessible as an argument `inputs`, similar to `pkgs` and `lib`, inside the configuration.
```nix
# in flake.nix
darwin.lib.darwinSystem {
system = "x86_64-darwin";
modules = [ ./configuration.nix ];
inputs = { inherit darwin dotfiles nixpkgs; };
specialArgs = { inherit inputs; };
}
# in configuration.nix:
{ pkgs, lib, inputs }:
# inputs.darwin, inputs.dotfiles, and inputs.nixpkgs can be accessed here
```
Alternatively, `specialArgs` could be used:
```nix
darwin.lib.darwinSystem {
system = "x86_64-darwin";
modules = [ ./configuration.nix ];
specialArgs = { inherit darwin dotfiles nixpkgs; };
}
# in configuration.nix:
{ pkgs, lib, darwin, dotfiles, nixpkgs }:
# in configuration.nix
{ pkgs, lib, inputs }:
# inputs.self, inputs.darwin, and inputs.nixpkgs can be accessed here
```
Since the installer doesn't work with flakes out of the box yet, nix-darwin will need to

View file

@ -6,19 +6,16 @@
}:
let
evalConfig = import ./eval-config.nix { inherit lib; };
eval = evalConfig {
inherit system;
modules = [ configuration nixpkgsRevisionModule ];
inputs = { inherit nixpkgs; };
eval = import ./eval-config.nix {
inherit lib;
modules = [
configuration
{ nixpkgs.source = lib.mkDefault nixpkgs; }
] ++ lib.optional (system != null) {
nixpkgs.system = lib.mkDefault system;
};
};
nixpkgsRevisionModule =
if nixpkgs?rev && lib.isString nixpkgs.rev
then { system.nixpkgsRevision = nixpkgs.rev; }
else { };
# The source code of this repo needed by the [un]installers.
nix-darwin = lib.cleanSource (
lib.cleanSourceWith {

View file

@ -1,13 +1,5 @@
{ lib }:
let
nixpkgs-lib = lib;
in
{ system ? builtins.currentSystem or "x86_64-darwin"
, pkgs ? null
, lib ? nixpkgs-lib
{ lib
, modules
, inputs
, baseModules ? import ./modules/module-list.nix
, specialArgs ? { }
, check ? true
@ -18,26 +10,13 @@ let
_file = ./eval-config.nix;
config = {
_module.args = {
inherit baseModules inputs modules;
inherit baseModules modules;
};
};
};
pkgsModule = { config, inputs, ... }: {
_file = ./eval-config.nix;
config = {
_module.args.pkgs = lib.mkIf (pkgs != null) (lib.mkForce pkgs);
nixpkgs.source = lib.mkDefault inputs.nixpkgs;
# This permits the configuration to override the passed-in
# system.
nixpkgs.system = lib.mkDefault system;
};
};
eval = lib.evalModules (builtins.removeAttrs args [ "lib" "inputs" "pkgs" "system" ] // {
modules = modules ++ [ argsModule pkgsModule ] ++ baseModules;
eval = lib.evalModules (builtins.removeAttrs args [ "lib" ] // {
modules = modules ++ [ argsModule ] ++ baseModules;
specialArgs = { modulesPath = builtins.toString ./modules; } // specialArgs;
});
in

View file

@ -4,22 +4,36 @@
outputs = { self, nixpkgs }: {
lib = {
# TODO handle multiple architectures.
evalConfig = import ./eval-config.nix { inherit (nixpkgs) lib; };
evalConfig = import ./eval-config.nix;
darwinSystem =
{ modules, inputs ? { }
, system ? throw "darwin.lib.darwinSystem now requires 'system' to be passed explicitly"
, ...
}@args:
self.lib.evalConfig (args // {
inherit system;
inputs = { inherit nixpkgs; darwin = self; } // inputs;
modules = modules ++ [ self.darwinModules.flakeOverrides ];
});
darwinSystem = args@{ modules, ... }: self.lib.evalConfig (
{ inherit (nixpkgs) lib; }
// nixpkgs.lib.optionalAttrs (args ? pkgs) { inherit (args.pkgs) lib; }
// builtins.removeAttrs args [ "system" "pkgs" "inputs" ]
// {
modules = modules
++ nixpkgs.lib.optional (args ? pkgs) ({ lib, ... }: {
_module.args.pkgs = lib.mkForce args.pkgs;
})
# Backwards compatibility shim; TODO: warn?
++ nixpkgs.lib.optional (args ? system) ({ lib, ... }: {
nixpkgs.system = lib.mkDefault args.system;
})
# Backwards compatibility shim; TODO: warn?
++ nixpkgs.lib.optional (args ? inputs) {
_module.args.inputs = args.inputs;
}
++ [ ({ lib, ... }: {
nixpkgs.source = lib.mkDefault nixpkgs;
system.checks.verifyNixPath = lib.mkDefault false;
system.darwinVersionSuffix = ".${self.shortRev or "dirty"}";
system.darwinRevision = lib.mkIf (self ? rev) self.rev;
}) ];
});
};
darwinModules.flakeOverrides = ./modules/system/flake-overrides.nix;
darwinModules.hydra = ./modules/examples/hydra.nix;
darwinModules.lnl = ./modules/examples/lnl.nix;
darwinModules.ofborg = ./modules/examples/ofborg.nix;
@ -32,8 +46,10 @@
checks = nixpkgs.lib.genAttrs ["aarch64-darwin" "x86_64-darwin"] (system: let
simple = self.lib.darwinSystem {
inherit system;
modules = [ self.darwinModules.simple ];
modules = [
self.darwinModules.simple
{ nixpkgs.hostPlatform = system; }
];
};
in {
simple = simple.system;

View file

@ -1,9 +1,9 @@
{
description = "Example darwin system flake";
description = "Example Darwin system flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
darwin.url = "github:lnl7/nix-darwin";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
darwin.url = "github:LnL7/nix-darwin";
darwin.inputs.nixpkgs.follows = "nixpkgs";
};
@ -30,6 +30,9 @@
# Used for backwards compatibility, please read the changelog before changing.
# $ darwin-rebuild changelog
system.stateVersion = 4;
# The platform the configuration will be used on.
nixpkgs.hostPlatform = "x86_64-darwin";
};
in
{
@ -37,7 +40,6 @@
# $ darwin-rebuild build --flake .#simple
darwinConfigurations."simple" = darwin.lib.darwinSystem {
modules = [ configuration ];
system = "x86_64-darwin";
};
# Expose the package set, including overlays, for convenience.

View file

@ -1,20 +0,0 @@
{ lib, inputs, ... }:
with lib;
let
inherit (inputs) darwin nixpkgs;
in
{
config = {
system.checks.verifyNixPath = mkDefault false;
system.checks.verifyNixChannels = mkDefault false;
system.darwinVersionSuffix = ".${darwin.shortRev or "dirty"}";
system.darwinRevision = mkIf (darwin ? rev) darwin.rev;
system.nixpkgsVersionSuffix = ".${substring 0 8 (nixpkgs.lastModifiedDate or nixpkgs.lastModified or "19700101")}.${nixpkgs.shortRev or "dirty"}";
system.nixpkgsRevision = mkIf (nixpkgs ? rev) nixpkgs.rev;
};
}