Skip to content

Commit f2f83da

Browse files
authored
Merge pull request #58 from emilazy/push-yykwxmksmmnu
Add migration logic to nix-darwin module
2 parents 3dd852a + 45c7b26 commit f2f83da

File tree

6 files changed

+141
-184
lines changed

6 files changed

+141
-184
lines changed

README.md

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ You can get started with Determinate in one of two ways:
1313
| Situation | How to install |
1414
| :----------------------------------- | :--------------------------------------------------------------------------- |
1515
| **Linux** but not using [NixOS] | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
16-
| **macOS** but not using [nix-darwin] | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
16+
| **macOS** | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
1717
| **Linux** and using [NixOS] | The [NixOS module](#nixos) provided by this flake |
18-
| **macOS** and using [nix-darwin] | The [nix-darwin module](#nix-darwin) provided by this flake |
1918

2019
## Installing using the Determinate Nix Installer
2120

2221
If you use...
2322

24-
- **macOS** (not [nix-darwin]) or
23+
- **macOS** (including [nix-darwin]) or
2524
- **Linux** (not [NixOS])
2625

2726
...you can install Determinate using the [Determinate Nix Installer][installer] with the `--determinate` flag:
@@ -33,7 +32,7 @@ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix
3332

3433
## Installing using our Nix flake
3534

36-
If you use [nix-darwin] or [NixOS] you can install Determinate using this [Nix flake][flakes].
35+
If you use [NixOS] you can install Determinate using this [Nix flake][flakes].
3736
To add the `determinate` flake as a [flake input][flake-inputs]:
3837

3938
```nix
@@ -44,9 +43,7 @@ To add the `determinate` flake as a [flake input][flake-inputs]:
4443

4544
> We recommend not using a [`follows`][follows] directive for [Nixpkgs] (`inputs.nixpkgs.follows = "nixpkgs"`) in conjunction with the Determinate flake, as it leads to cache misses for artifacts otherwise available from [FlakeHub Cache][cache].
4645
47-
### NixOS
48-
49-
If you're a [NixOS] user, you can quickly set up Determinate using the `nixosModules.default` module output from this flake.
46+
You can quickly set up Determinate using the `nixosModules.default` module output from this flake.
5047
Here's an example NixOS configuration:
5148

5249
```nix
@@ -66,28 +63,6 @@ Here's an example NixOS configuration:
6663
}
6764
```
6865

69-
### nix-darwin
70-
71-
If you're a [nix-darwin] user on macOS, you can quickly set up Determinate using the `darwinModules.default` module output from this flake.
72-
Here's an example nix-darwin configuration:
73-
74-
```nix
75-
{
76-
inputs.determinate.url = "https://flakehub.com/f/DeterminateSystems/determinate/0.1";
77-
inputs.nix-darwin.url = "github:LnL7/nix-darwin";
78-
79-
outputs = { determinate, nix-darwin, ... }: {
80-
darwinConfigurations.my-workstation-aarch64-darwin = nix-darwin.lib.darwinSystem {
81-
system = "aarch64-darwin";
82-
modules = [
83-
# Load the Determinate module
84-
determinate.darwinModules.default
85-
];
86-
};
87-
};
88-
}
89-
```
90-
9166
[actions]: https://github.com/features/actions
9267
[cache]: https://determinate.systems/posts/flakehub-cache-beta
9368
[det-nix]: https://determinate.systems/nix

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
};
6363
});
6464

65-
darwinModules.default = import ./modules/nix-darwin.nix inputs;
65+
# Stub for migration.
66+
darwinModules.default = ./modules/nix-darwin.nix;
6667

6768
nixosModules.default = import ./modules/nixos.nix inputs;
6869
};

modules/nix-darwin.nix

Lines changed: 76 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,89 @@
1-
inputs:
2-
{ lib, config, pkgs, ... }:
1+
{ lib, options, ... }:
32
let
4-
inherit (import ./shared.nix inputs)
5-
commonNixSettingsModule
6-
restrictedNixSettingsModule
7-
mkPreferable
8-
;
9-
in
10-
{
11-
imports = [
12-
commonNixSettingsModule
13-
restrictedNixSettingsModule
14-
];
15-
16-
config = {
17-
# Make Nix use the Nix daemon
18-
nix.useDaemon = true;
19-
20-
# Make sure that the user can't enable the nix-daemon in their own nix-darwin config
21-
services.nix-daemon.enable = lib.mkForce false;
22-
23-
system.activationScripts.nix-daemon = lib.mkForce { enable = false; text = ""; };
24-
system.activationScripts.launchd.text = lib.mkBefore ''
25-
if test -e /Library/LaunchDaemons/org.nixos.nix-daemon.plist; then
26-
echo "Unloading org.nixos.nix-daemon"
27-
launchctl bootout system /Library/LaunchDaemons/org.nixos.nix-daemon.plist || true
28-
mv /Library/LaunchDaemons/org.nixos.nix-daemon.plist /Library/LaunchDaemons/.before-determinate-nixd.org.nixos.nix-daemon.plist.skip
29-
fi
3+
postMigrationInstructions = ''
4+
You have successfully migrated your Determinate installation.
5+
Please remove `determinate.darwinModules.default` from your
6+
nix-darwin configuration, and ensure that you have nix-darwin’s own
7+
Nix installation management disabled by setting:
308
31-
if test -e /Library/LaunchDaemons/org.nixos.darwin-store.plist; then
32-
echo "Unloading org.nixos.darwin-store"
33-
launchctl bootout system /Library/LaunchDaemons/org.nixos.darwin-store.plist || true
34-
mv /Library/LaunchDaemons/org.nixos.darwin-store.plist /Library/LaunchDaemons/.before-determinate-nixd.org.nixos.darwin-store.plist.skip
35-
fi
9+
nix.enable = false;
3610
37-
install -d -m 755 -o root -g wheel /usr/local/bin
38-
cp ${inputs.self.packages.${pkgs.stdenv.system}.default}/bin/determinate-nixd /usr/local/bin/.determinate-nixd.next
39-
chmod +x /usr/local/bin/.determinate-nixd.next
40-
mv /usr/local/bin/.determinate-nixd.next /usr/local/bin/determinate-nixd
41-
'';
11+
Then run `darwin-rebuild switch` again.
12+
'';
13+
in
14+
{
15+
config =
16+
# Check if nix-darwin is new enough for the `nix.enable` option.
17+
if options.nix.enable.visible or true then
18+
{
19+
nix.enable = false;
4220

43-
launchd.daemons.determinate-nixd-store.serviceConfig = {
44-
Label = "systems.determinate.nix-store";
45-
RunAtLoad = true;
21+
system.activationScripts.checks.text = lib.mkBefore ''
22+
if [[ ! -e /usr/local/bin/determinate-nixd ]]; then
23+
printf >&2 '\e[1;31merror: Determinate not installed, aborting activation\e[0m\n'
24+
printf >&2 'The Determinate nix-darwin module is no longer necessary. To install\n'
25+
printf >&2 'Determinate, remove `determinate.darwinModules.default` from your\n'
26+
printf >&2 'configuration and follow the installation installations at\n'
27+
printf >&2 '<https://docs.determinate.systems/getting-started/individuals>.\n'
28+
exit 2
29+
fi
4630
47-
StandardErrorPath = lib.mkForce "/var/log/determinate-nix-init.log";
48-
StandardOutPath = lib.mkForce "/var/log/determinate-nix-init.log";
31+
# Hack: Detect the version of the `.plist` set up by the old
32+
# version of the module.
33+
if grep -- '--nix-bin' /Library/LaunchDaemons/systems.determinate.nix-daemon.plist >/dev/null; then
34+
printf >&2 '\e[1;31merror: Determinate needs migration, aborting activation\e[0m\n'
35+
printf >&2 'Determinate now manages the Nix installation independently of the\n'
36+
printf >&2 'nix-darwin module.\n'
37+
printf >&2 '\n'
38+
printf >&2 'Please download and run the macOS installer from\n'
39+
printf >&2 '<https://docs.determinate.systems/getting-started/individuals> and then\n'
40+
printf >&2 'run `darwin-rebuild switch` again to migrate your installation.\n'
41+
exit 2
42+
fi
4943
50-
ProgramArguments = lib.mkForce [
51-
"/usr/local/bin/determinate-nixd"
52-
"--nix-bin"
53-
"${config.nix.package}/bin"
54-
"init"
55-
];
56-
};
44+
if [[ ! -e /run/current-system/Library/LaunchDaemons/systems.determinate.nix-daemon.plist ]]; then
45+
printf >&2 '\e[1;31merror: deprecated Determinate module present, aborting activation\e[0m\n'
46+
printf >&2 '%s' ${lib.escapeShellArg postMigrationInstructions}
47+
exit 2
48+
fi
49+
'';
5750

58-
launchd.daemons.determinate-nixd.serviceConfig = {
59-
Label = "systems.determinate.nix-daemon";
51+
system.activationScripts.extraActivation.text = lib.mkBefore ''
52+
# Hack: Make sure nix-darwin doesn’t clobber the Determinate
53+
# launchd daemons after they become unmanaged.
6054
61-
StandardErrorPath = lib.mkForce "/var/log/determinate-nix-daemon.log";
62-
StandardOutPath = lib.mkForce "/var/log/determinate-nix-daemon.log";
55+
determinateDaemonsStash=$(mktemp -d --suffix=determinate-daemons)
56+
cp -a /Library/LaunchDaemons/systems.determinate.{nix-daemon,nix-store}.plist "$determinateDaemonsStash"
6357
64-
ProgramArguments = lib.mkForce [
65-
"/usr/local/bin/determinate-nixd"
66-
"--nix-bin"
67-
"${config.nix.package}/bin"
68-
"daemon"
69-
];
58+
# shellcheck disable=SC2317
59+
restoreDeterminateDaemons() {
60+
printf >&2 'restoring Determinate daemons...\n'
61+
mv "$determinateDaemonsStash"/*.plist /Library/LaunchDaemons
62+
rmdir "$determinateDaemonsStash"
63+
launchctl load -w /Library/LaunchDaemons/systems.determinate.nix-daemon.plist
64+
launchctl load -w /Library/LaunchDaemons/systems.determinate.nix-store.plist
65+
printf >&2 '\n'
66+
printf >&2 '%s' ${lib.escapeShellArg postMigrationInstructions}
67+
}
7068
71-
Sockets = {
72-
"determinate-nixd.socket" = {
73-
# We'd set `SockFamily = "Unix";`, but nix-darwin automatically sets it with SockPathName
74-
SockPassive = true;
75-
SockPathName = "/var/run/determinate-nixd.socket";
76-
};
69+
trap restoreDeterminateDaemons EXIT
70+
'';
71+
}
72+
else
73+
{
74+
assertions = [
75+
{
76+
assertion = false;
77+
message = ''
78+
Determinate now manages the Nix installation independently of
79+
the nix-darwin module.
7780
78-
"nix-daemon.socket" = {
79-
# We'd set `SockFamily = "Unix";`, but nix-darwin automatically sets it with SockPathName
80-
SockPassive = true;
81-
SockPathName = "/var/run/nix-daemon.socket";
82-
};
83-
};
84-
85-
SoftResourceLimits = {
86-
NumberOfFiles = mkPreferable 1048576;
87-
NumberOfProcesses = mkPreferable 1048576;
88-
Stack = mkPreferable 67108864;
89-
};
90-
HardResourceLimits = {
91-
NumberOfFiles = mkPreferable 1048576;
92-
NumberOfProcesses = mkPreferable 1048576;
93-
Stack = mkPreferable 67108864;
81+
Please download and run the macOS installer from
82+
<https://docs.determinate.systems/getting-started>,
83+
update nix-darwin, and then run `darwin-rebuild switch`
84+
again to migrate your installation.
85+
'';
86+
}
87+
];
9488
};
95-
};
96-
};
9789
}

modules/nixos.nix

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,64 @@
11
inputs:
22
{ lib, pkgs, config, ... }:
33
let
4-
inherit (import ./shared.nix inputs)
5-
commonNixSettingsModule
6-
restrictedNixSettingsModule
7-
mkPreferable
8-
mkMorePreferable
9-
;
4+
# Stronger than mkDefault (1000), weaker than mkForce (50) and the "default override priority"
5+
# (100).
6+
mkPreferable = lib.mkOverride 750;
7+
8+
# Stronger than the "default override priority", as the upstream module uses that, and weaker than mkForce (50).
9+
mkMorePreferable = lib.mkOverride 75;
10+
11+
# The settings configured in this module must be generally settable by users both trusted and
12+
# untrusted by the Nix daemon. Settings that require being a trusted user belong in the
13+
# `restrictedSettingsModule` below.
14+
commonNixSettingsModule = { config, pkgs, lib, ... }: {
15+
nix.package = inputs.nix.packages."${pkgs.stdenv.system}".default;
16+
17+
nix.registry.nixpkgs = {
18+
exact = true;
19+
20+
from = {
21+
type = "indirect";
22+
id = "nixpkgs";
23+
};
24+
25+
# NOTE(cole-h): The NixOS module exposes a `flake` option that is a fancy wrapper around
26+
# setting `to` -- we don't want to clobber this if users have set it on their own
27+
to = lib.mkIf (config.nix.registry.nixpkgs.flake or null == null) (mkPreferable {
28+
type = "tarball";
29+
url = "https://flakehub.com/f/DeterminateSystems/nixpkgs-weekly/0.1.0.tar.gz";
30+
});
31+
};
32+
33+
nix.settings = {
34+
bash-prompt-prefix = "(nix:$name)\\040";
35+
extra-experimental-features = [ "nix-command" "flakes" ];
36+
extra-nix-path = [ "nixpkgs=flake:nixpkgs" ];
37+
extra-substituters = [ "https://cache.flakehub.com" ];
38+
};
39+
};
40+
41+
# The settings configured in this module require being a user trusted by the Nix daemon.
42+
restrictedNixSettingsModule = { ... }: {
43+
nix.settings = restrictedNixSettings;
44+
};
45+
46+
# Nix settings that require being a trusted user to configure.
47+
restrictedNixSettings = {
48+
always-allow-substitutes = true;
49+
netrc-file = "/nix/var/determinate/netrc";
50+
upgrade-nix-store-path-url = "https://install.determinate.systems/nix-upgrade/stable/universal";
51+
extra-trusted-public-keys = [
52+
"cache.flakehub.com-3:hJuILl5sVK4iKm86JzgdXW12Y2Hwd5G07qKtHTOcDCM="
53+
"cache.flakehub.com-4:Asi8qIv291s0aYLyH6IOnr5Kf6+OF14WVjkE6t3xMio="
54+
"cache.flakehub.com-5:zB96CRlL7tiPtzA9/WKyPkp3A2vqxqgdgyTVNGShPDU="
55+
"cache.flakehub.com-6:W4EGFwAGgBj3he7c5fNh9NkOXw0PUVaxygCVKeuvaqU="
56+
"cache.flakehub.com-7:mvxJ2DZVHn/kRxlIaxYNMuDG1OvMckZu32um1TadOR8="
57+
"cache.flakehub.com-8:moO+OVS0mnTjBTcOUh2kYLQEd59ExzyoW1QgQ8XAARQ="
58+
"cache.flakehub.com-9:wChaSeTI6TeCuV/Sg2513ZIM9i0qJaYsF+lZCXg0J6o="
59+
"cache.flakehub.com-10:2GqeNlIp6AKp4EF2MVbE1kBOp9iBSyo0UPR9KoR0o1Y="
60+
];
61+
};
1062
in
1163
{
1264
imports = [

modules/shared.nix

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
system = "aarch64-darwin";
3333

3434
modules = [
35-
determinate.darwinModules.default
3635
{
36+
nix.enable = false;
3737
system.stateVersion = 5;
3838
}
3939
];

0 commit comments

Comments
 (0)