Skip to content

Commit 60b33c3

Browse files
authored
Merge pull request #104 from DeterminateSystems/nix-darwin-custom-conf
Provide nix-darwin module for nix.custom.conf
2 parents 7afeca4 + 402d69d commit 60b33c3

File tree

6 files changed

+195
-10
lines changed

6 files changed

+195
-10
lines changed

README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Determinate has two core components:
1010

1111
You can get started with Determinate in one of two ways:
1212

13-
| Situation | How to install |
14-
| :----------------------------------- | :--------------------------------------------------------------------------- |
15-
| **Linux** but not using [NixOS] | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
16-
| **macOS** | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
17-
| **Linux** and using [NixOS] | The [NixOS module](#nixos) provided by this flake |
13+
| Situation | How to install |
14+
| :------------------------------ | :--------------------------------------------------------------------------- |
15+
| **Linux** but not using [NixOS] | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
16+
| **macOS** | [Determinate Nix Installer](#installing-using-the-determinate-nix-installer) |
17+
| **Linux** and using [NixOS] | The [NixOS module](#nixos) provided by this flake |
1818

1919
## Installing using the Determinate Nix Installer
2020

@@ -62,8 +62,73 @@ Here's an example NixOS configuration for the current stable NixOS:
6262
}
6363
```
6464

65+
## nix-darwin
66+
67+
If you use [nix-darwin] to provide Nix-based configuration for your macOS system, you need to disable nix-darwin's built-in Nix configuration mechanisms by setting `nix.enable = false`; if not, Determinate Nix **does not work properly**.
68+
Here's an example nix-darwin configuration that would be compatible with Determinate Nix:
69+
70+
```nix
71+
{
72+
inputs.determinate.url = "https://flakehub.com/f/DeterminateSystems/determinate/0";
73+
inputs.nix-darwin = {
74+
url = "https://flakehub.com/f/nix-darwin/nix-darwin/0";
75+
inputs.nixpkgs.follows = "nixpkgs";
76+
};
77+
inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0";
78+
79+
outputs = { determinate, nixpkgs, ... }: {
80+
darwinConfigurations."my-username-aarch64-darwin" = inputs.nix-darwin.lib.darwinSystem {
81+
inherit system;
82+
modules = [
83+
({ ... }: {
84+
# Let Determinate Nix handle Nix configuration rather than nix-darwin
85+
nix.enable = false;
86+
87+
# Other nix-darwin settings
88+
})
89+
];
90+
};
91+
};
92+
}
93+
```
94+
95+
While Determinate Nix creates and manages the standard `nix.conf` file for you, you can set custom configuration in the `/etc/nix/nix.custom.conf` file, which is explained in more detail [in our documentation][configuring-determinate-nix].
96+
If you'd like to set that custom configuration using nix-darwin, you can use this `determinate` flake for that.
97+
Here's an example nix-darwin configuration that writes custom settings:
98+
99+
```nix
100+
{
101+
inputs.determinate.url = "https://flakehub.com/f/DeterminateSystems/determinate/0";
102+
inputs.nix-darwin = {
103+
url = "https://flakehub.com/f/nix-darwin/nix-darwin/0";
104+
inputs.nixpkgs.follows = "nixpkgs";
105+
};
106+
inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0";
107+
108+
outputs = { determinate, nixpkgs, ... }: {
109+
darwinConfigurations."my-username-aarch64-darwin" = inputs.nix-darwin.lib.darwinSystem {
110+
inherit system;
111+
modules = [
112+
# Add the determinate nix-darwin module
113+
inputs.determinate.darwinModules.default
114+
({ ... }: {
115+
# Let Determinate Nix handle Nix configuration rather than nix-darwin
116+
nix.enable = false;
117+
118+
# Custom settings written to /etc/nix/nix.custom.conf
119+
determinate-nix.customSettings = {
120+
flake-registry = "/etc/nix/flake-registry.json";
121+
};
122+
})
123+
];
124+
};
125+
};
126+
}
127+
```
128+
65129
[actions]: https://github.com/features/actions
66130
[cache]: https://determinate.systems/posts/flakehub-cache-beta
131+
[configuring-determinate-nix]: https://docs.determinate.systems/determinate-nix#determinate-nix-configuration
67132
[det-nix]: https://determinate.systems/nix
68133
[detsys]: https://determinate.systems
69134
[dnixd]: https://docs.determinate.systems/determinate-nix#determinate-nixd
@@ -78,7 +143,7 @@ Here's an example NixOS configuration for the current stable NixOS:
78143
[netrc]: https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html
79144
[nix]: https://zero-to-nix.com/concepts/nix
80145
[nix-conf]: https://nix.dev/manual/nix/latest/command-ref/conf-file
81-
[nix-darwin]: https://github.com/LnL7/nix-darwin
146+
[nix-darwin]: https://github.com/nix-darwin/nix-darwin
82147
[nixos]: https://zero-to-nix.com/concepts/nixos
83148
[nixpkgs]: https://zero-to-nix.com/concepts/nixpkgs
84149
[pkg]: https://install.determinate.systems/determinate-pkg/stable/Universal

flake.nix

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@
7676
}
7777
);
7878

79-
# Stub for migration.
80-
darwinModules.default = ./modules/nix-darwin.nix;
79+
formatter = forEachSupportedSystem ({ pkgs, ... }: pkgs.nixfmt-rfc-style);
80+
81+
darwinModules = {
82+
default = ./modules/nix-darwin/default.nix;
83+
84+
# In case we come across anyone who still needs to migrate
85+
migration = ./modules/nix-darwin/migration.nix;
86+
};
8187

8288
nixosModules.default = import ./modules/nixos.nix inputs;
8389
};

modules/nix-darwin/config/LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2017 <Daiderd Jordan>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This method of generating Nix configuration borrows heavily from the nix-darwin project:
2+
# https://github.com/nix-darwin/nix-darwin/blob/e04a388232d9a6ba56967ce5b53a8a6f713cdfcf/modules/nix/default.nix
3+
# We have included the LICENSE file for the nix-darwin project in this directory from the e04a388232d9a6ba56967ce5b53a8a6f713cdfcf revision of the project:
4+
# https://github.com/nix-darwin/nix-darwin/tree/e04a388232d9a6ba56967ce5b53a8a6f713cdfcf
5+
# https://github.com/nix-darwin/nix-darwin/blob/e04a388232d9a6ba56967ce5b53a8a6f713cdfcf/LICENSE
6+
7+
{ lib }:
8+
9+
let
10+
inherit (lib) types;
11+
12+
mkValueString =
13+
v:
14+
if v == null then
15+
""
16+
else if builtins.isBool v then
17+
lib.boolToString v
18+
else if builtins.isInt v then
19+
builtins.toString v
20+
else if builtins.isFloat v then
21+
lib.strings.floatToString v
22+
else if builtins.isList v then
23+
builtins.toJSON v
24+
else if lib.isDerivation v then
25+
builtins.toString v
26+
else if builtins.isPath v then
27+
builtins.toString v
28+
else if builtins.isAttrs v then
29+
builtins.toJSON v
30+
else if builtins.isString v then
31+
v
32+
else if lib.strings.isCoercibleToString v then
33+
builtins.toString v
34+
else
35+
abort "The Nix configuration value ${lib.generators.toPretty { } v} can't be encoded";
36+
37+
mkKeyValue = k: v: "${lib.escape [ "=" ] k} = ${mkValueString v}";
38+
in
39+
{
40+
mkCustomConfig = attrs: lib.mapAttrsToList mkKeyValue attrs;
41+
}

modules/nix-darwin/default.nix

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{ config, lib, ... }:
2+
3+
let
4+
inherit (lib) types;
5+
6+
inherit (import ./config/config.nix { inherit lib; }) mkCustomConfig;
7+
8+
semanticConfType =
9+
with types;
10+
let
11+
confAtom =
12+
nullOr (oneOf [
13+
bool
14+
int
15+
float
16+
str
17+
path
18+
package
19+
])
20+
// {
21+
description = "Nix configuration atom (null, Boolean, integer, float, list, derivation, path, attribute set)";
22+
};
23+
in
24+
attrsOf (either confAtom (listOf confAtom));
25+
26+
# Settings that Determinate Nix handles for you
27+
disallowedOptions = [
28+
"always-allow-substitutes"
29+
"bash-prompt-prefix"
30+
"netrc-file"
31+
"ssl-cert-file"
32+
"upgrade-nix-store-path-url"
33+
];
34+
in
35+
{
36+
options.determinate-nix.customSettings = lib.mkOption {
37+
type = types.submodule {
38+
options = { };
39+
40+
# Support "free-form" options
41+
freeformType = semanticConfType;
42+
};
43+
};
44+
45+
config = lib.mkIf (config.determinate-nix.customSettings != { }) {
46+
assertions = [
47+
{
48+
assertion = lib.all (key: !lib.hasAttr key config.determinate-nix.customSettings) disallowedOptions;
49+
message = ''
50+
These settings are not allowed in `determinate-nix.customSettings`:
51+
${lib.concatStringsSep ", " disallowedOptions}
52+
'';
53+
}
54+
];
55+
56+
environment.etc."nix/nix.custom.conf".text = lib.concatStringsSep "\n" (
57+
[
58+
"# This custom configuration file for Determinate Nix is generated by the determinate module for nix-darwin."
59+
"# Update your custom settings by changing your nix-darwin configuration, not by modifying this file directly."
60+
""
61+
]
62+
++ mkCustomConfig config.determinate-nix.customSettings
63+
);
64+
};
65+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
config,
55
...
66
}:
7+
78
let
89
cfg = config.determinate;
910

1011
postMigrationInstructions = ''
1112
You have successfully migrated your Determinate installation.
1213
Please remove `determinate.darwinModules.default` from your
13-
nix-darwin configuration, and ensure that you have nix-darwins own
14+
nix-darwin configuration, and ensure that you have nix-darwin's own
1415
Nix installation management disabled by setting:
1516
1617
nix.enable = false;
@@ -62,7 +63,7 @@ in
6263
'';
6364

6465
system.activationScripts.extraActivation.text = lib.mkBefore ''
65-
# Hack: Make sure nix-darwin doesnt clobber the Determinate
66+
# Hack: Make sure nix-darwin doesn't clobber the Determinate
6667
# launchd daemons after they become unmanaged.
6768
6869
determinateDaemonsStash=$(mktemp -d --suffix=determinate-daemons)

0 commit comments

Comments
 (0)