├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── flake.lock
├── flake.nix
├── nix
├── flake-checks.nix
├── flake-output.nix
├── mk-darwin-flake.nix
├── modules
│ ├── activation-diff.nix
│ ├── darwin-rebuild-overlay.nix
│ ├── home-manager.nix
│ ├── home-manager
│ │ └── init-shell-path.nix
│ ├── intel-overlay.nix
│ ├── mk-host.nix
│ ├── mk-user.nix
│ └── niv-managed-dmg-overlay.nix
└── overlays
│ ├── darwin-rebuild.nix
│ ├── intel-pkgs.nix
│ └── niv-managed-dmg-apps.nix
└── templates
└── minimal
├── .gitignore
├── Makefile
├── README.md
├── flake.nix
├── nix.conf
└── nix
├── homeConfigurations
└── your-username.nix
├── hostConfigurations
└── your-hostname.nix
├── sources.json
└── sources.nix
/.gitignore:
--------------------------------------------------------------------------------
1 | result*
2 |
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: help
2 | help:
3 | @cat $(MAKEFILE_LIST) | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
4 |
5 | .PHONY: update
6 | update: ## Update flake sources.
7 | nix flake update
8 |
9 | .PHONY: test
10 | test: ## Test system is installable.
11 | nix flake check
12 | nix run '.#format' -- --check .
13 |
14 | .PHONY: format
15 | format: ## Format all nix files.
16 | nix run '.#format' -- --quiet .
17 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Deprecation Notice
2 |
3 | NOTE: This repo is now read-only, since I (vic) am no longer using MacOS since early 2023. You can still find a much more maintained and better alternative at
4 | https://github.com/srid/nixos-flake
5 |
6 |
7 |
8 | Old README
9 |
10 |
11 |
12 |
13 |
14 | ## Usage
15 |
16 | * Create your system flake:
17 |
18 | ``` sh
19 | nix flake new my-system --template github:vic/mk-darwin-system#minimal
20 |
21 | cd my-system
22 | git init
23 | git add .
24 | ```
25 |
26 | * Change the values of `hostName` and `userName` inside `flake.nix`.
27 |
28 | * Create files inside `nix/hostConfigurations` and `nix/homeConfigurations`.
29 |
30 | * See if everything is ok by running: `nix flake check`
31 |
32 | * Activate your system running: `nix run`
33 |
34 |
35 | ## Examples
36 |
37 | * [vic](http://twitter.com/oeiuwq)'s own [M1 system](http://github.com/vic/vix) from which
38 | this library was extracted.
39 |
40 | * yours, [send a pull-request](http://github.com/vic/mk-darwin-system/pulls).
41 |
42 | ## References
43 |
44 | * Files inside `nix/hostConfigurations` can set any [nix-darwin option](https://daiderd.com/nix-darwin/manual/index.html#sec-options).
45 | * Files inside `nix/homeConfigurations` can set any [home-manager option](https://nix-community.github.io/home-manager/options.html).
46 | * Using [nix-darwin brew options](https://daiderd.com/nix-darwin/manual/index.html#opt-homebrew.enable) it's possible to install
47 | packages from [Homebrew](https://brew.sh/) taps, casks, [Mac App Store Apps](https://www.apple.com/app-store/) and Docker containers. Note that these would be installed as system packages.
48 | * When your nix files get bigger you can split them using conventional [NixOS Modules](https://nixos.wiki/wiki/NixOS_modules) `imports = []`.
49 | * Be sure to also check this nice [Nix language tutorial](https://nix.dev/tutorials/nix-language)
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/flake.lock:
--------------------------------------------------------------------------------
1 | {
2 | "nodes": {
3 | "flake-utils": {
4 | "locked": {
5 | "lastModified": 1659877975,
6 | "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
7 | "owner": "numtide",
8 | "repo": "flake-utils",
9 | "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
10 | "type": "github"
11 | },
12 | "original": {
13 | "owner": "numtide",
14 | "repo": "flake-utils",
15 | "type": "github"
16 | }
17 | },
18 | "home-manager": {
19 | "inputs": {
20 | "nixpkgs": [
21 | "nixpkgs"
22 | ],
23 | "utils": "utils"
24 | },
25 | "locked": {
26 | "lastModified": 1665271265,
27 | "narHash": "sha256-4Nn0T5YoR3bBLFnPy6Tkc8zzmzMTBjSGZq05c5hKhEI=",
28 | "owner": "nix-community",
29 | "repo": "home-manager",
30 | "rev": "e1f1160284198a68ea8c7fffbbb1436f99e46ef9",
31 | "type": "github"
32 | },
33 | "original": {
34 | "owner": "nix-community",
35 | "repo": "home-manager",
36 | "type": "github"
37 | }
38 | },
39 | "nix-darwin": {
40 | "inputs": {
41 | "nixpkgs": [
42 | "nixpkgs"
43 | ]
44 | },
45 | "locked": {
46 | "lastModified": 1664210064,
47 | "narHash": "sha256-df6nKVZe/yAhmJ9csirTPahc0dldwm3HBhCVNA6qWr0=",
48 | "owner": "lnl7",
49 | "repo": "nix-darwin",
50 | "rev": "02d2551c927b7d65ded1b3c7cd13da5cc7ae3fcf",
51 | "type": "github"
52 | },
53 | "original": {
54 | "owner": "lnl7",
55 | "repo": "nix-darwin",
56 | "type": "github"
57 | }
58 | },
59 | "nixpkgs": {
60 | "locked": {
61 | "lastModified": 1665332913,
62 | "narHash": "sha256-ooqwRVwKVYL3m62dmRlfakndFiVrAYjkEjtsxYLQda4=",
63 | "owner": "NixOS",
64 | "repo": "nixpkgs",
65 | "rev": "6b0b91fee3c01d16a84f9b84e2730f77b7e76bc1",
66 | "type": "github"
67 | },
68 | "original": {
69 | "owner": "NixOS",
70 | "ref": "master",
71 | "repo": "nixpkgs",
72 | "type": "github"
73 | }
74 | },
75 | "root": {
76 | "inputs": {
77 | "flake-utils": "flake-utils",
78 | "home-manager": "home-manager",
79 | "nix-darwin": "nix-darwin",
80 | "nixpkgs": "nixpkgs"
81 | }
82 | },
83 | "utils": {
84 | "locked": {
85 | "lastModified": 1659877975,
86 | "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
87 | "owner": "numtide",
88 | "repo": "flake-utils",
89 | "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
90 | "type": "github"
91 | },
92 | "original": {
93 | "owner": "numtide",
94 | "repo": "flake-utils",
95 | "type": "github"
96 | }
97 | }
98 | },
99 | "root": "root",
100 | "version": 7
101 | }
102 |
--------------------------------------------------------------------------------
/flake.nix:
--------------------------------------------------------------------------------
1 | {
2 | inputs = {
3 | nixpkgs.url = "github:NixOS/nixpkgs/master";
4 |
5 | flake-utils.url = "github:numtide/flake-utils";
6 |
7 | nix-darwin.url = "github:lnl7/nix-darwin";
8 | nix-darwin.inputs.nixpkgs.follows = "nixpkgs";
9 |
10 | home-manager.url = "github:nix-community/home-manager";
11 | home-manager.inputs.nixpkgs.follows = "nixpkgs";
12 | };
13 |
14 | outputs = inputs: import ./nix/flake-output.nix inputs;
15 | }
16 |
--------------------------------------------------------------------------------
/nix/flake-checks.nix:
--------------------------------------------------------------------------------
1 | {
2 | self,
3 | nixpkgs,
4 | ...
5 | } @ inputs: let
6 | system = "aarch64-darwin";
7 |
8 | fromTemplate = name: let
9 | r = self.templates.${name}.path;
10 | f = import "${r}/flake.nix";
11 | x = f.outputs (inputs
12 | // {
13 | self = r;
14 | mk-darwin-system = self;
15 | });
16 | in
17 | x.checks.${system}.default;
18 |
19 | checkTemplates = builtins.mapAttrs (n: v: fromTemplate n) self.templates;
20 | in {
21 | ${system} = checkTemplates;
22 | }
23 |
--------------------------------------------------------------------------------
/nix/flake-output.nix:
--------------------------------------------------------------------------------
1 | {
2 | self,
3 | nixpkgs,
4 | flake-utils,
5 | ...
6 | } @ inputs: {
7 | mkFlake = import ./mk-darwin-flake.nix inputs;
8 |
9 | templates = rec {
10 | default = minimal;
11 |
12 | minimal = {
13 | description = "mkDarwinSystem minimal example";
14 | path = ./../templates/minimal;
15 | };
16 | };
17 |
18 | checks = import ./flake-checks.nix inputs;
19 |
20 | apps.aarch64-darwin = let
21 | pkgs = import nixpkgs {system = "aarch64-darwin";};
22 | in {
23 | format = flake-utils.lib.mkApp {drv = pkgs.alejandra;};
24 | };
25 | }
26 |
--------------------------------------------------------------------------------
/nix/mk-darwin-flake.nix:
--------------------------------------------------------------------------------
1 | {
2 | nixpkgs,
3 | flake-utils,
4 | nix-darwin,
5 | home-manager,
6 | ...
7 | } @ mkdw-inputs: {
8 | inputs ? mkdw-inputs,
9 | flake,
10 | hostName,
11 | hostModules ? ["${flake}/nix/hostConfigurations/${hostName}.nix"],
12 | userName,
13 | userModules ? ["${flake}/nix/homeConfigurations/${userName}.nix"],
14 | userHome ? "/Users/${userName}",
15 | rootModules ? [],
16 | }: let
17 | flakeInputs =
18 | mkdw-inputs
19 | // inputs
20 | // {
21 | inherit flake;
22 | nivSources = let
23 | f = "${flake}/nix/sources.nix";
24 | in
25 | if builtins.pathExists f
26 | then import f
27 | else {};
28 | };
29 |
30 | modules = [
31 | {_module.args.flake = flake;}
32 | {imports = rootModules;}
33 | (import ./modules/mk-host.nix {inherit hostName hostModules;})
34 | (import ./modules/mk-user.nix {inherit userName userHome userModules;})
35 | ./modules/activation-diff.nix
36 | ./modules/darwin-rebuild-overlay.nix
37 | ./modules/intel-overlay.nix
38 | ./modules/home-manager.nix
39 | ./modules/niv-managed-dmg-overlay.nix
40 | home-manager.darwinModules.home-manager
41 | ];
42 |
43 | darwin = nix-darwin.lib.darwinSystem {
44 | inherit modules;
45 | system = "aarch64-darwin";
46 | inputs = flakeInputs;
47 | };
48 |
49 | perSystem = flake-utils.lib.eachSystem ["aarch64-darwin"] (system:
50 | with darwin.pkgs; {
51 | packages.default = darwin.system;
52 | apps.default = flake-utils.lib.mkApp {drv = darwin-rebuild;};
53 | apps.format = flake-utils.lib.mkApp {drv = alejandra;};
54 | checks.default = darwin.system;
55 | devShells.default = mkShell {
56 | buildInputs = [nixVersions.stable niv alejandra];
57 | };
58 | });
59 |
60 | global = {
61 | darwinConfigurations.${hostName} = darwin;
62 | };
63 | in
64 | global // perSystem
65 |
--------------------------------------------------------------------------------
/nix/modules/activation-diff.nix:
--------------------------------------------------------------------------------
1 | {
2 | config,
3 | lib,
4 | pkgs,
5 | ...
6 | }: {
7 | system.activationScripts.diffClosures.text = ''
8 | if [ -e /run/current-system ]; then
9 | echo "new configuration diff" >&2
10 | $DRY_RUN_CMD ${config.nix.package}/bin/nix store \
11 | --experimental-features 'nix-command' \
12 | diff-closures /run/current-system "$systemConfig" \
13 | | sed -e 's/^/[diff]\t/' >&2
14 | fi
15 | '';
16 |
17 | system.activationScripts.preActivation.text =
18 | config.system.activationScripts.diffClosures.text;
19 | }
20 |
--------------------------------------------------------------------------------
/nix/modules/darwin-rebuild-overlay.nix:
--------------------------------------------------------------------------------
1 | {
2 | config,
3 | flake,
4 | ...
5 | }: {
6 | nixpkgs.overlays = [
7 | (import ./../overlays/darwin-rebuild.nix config.system flake)
8 | ];
9 | }
10 |
--------------------------------------------------------------------------------
/nix/modules/home-manager.nix:
--------------------------------------------------------------------------------
1 | {
2 | config,
3 | inputs,
4 | ...
5 | }: {
6 | home-manager = {
7 | useGlobalPkgs = true;
8 | useUserPackages = true;
9 |
10 | sharedModules = [
11 | ./home-manager/init-shell-path.nix
12 | ];
13 | };
14 | }
15 |
--------------------------------------------------------------------------------
/nix/modules/home-manager/init-shell-path.nix:
--------------------------------------------------------------------------------
1 | {
2 | system-config,
3 | config,
4 | lib,
5 | pkgs,
6 | ...
7 | }: let
8 | sysBin = "${system-config.system.profile}/sw/bin";
9 | username = config.home.username;
10 | usrBin = "${config.home.profileDirectory}/bin";
11 | in
12 | with lib; {
13 | config = mkMerge [
14 | (mkIf config.programs.bash.enable {
15 | programs.bash.profileExtra = ''
16 | case :$PATH: in
17 | *:${sysBin}:*) ;; # do nothing
18 | *) PATH=${sysBin}:$PATH ;;
19 | esac
20 | case :$PATH: in
21 | *:${usrBin}:*) ;; # do nothing
22 | *) PATH=${usrBin}:$PATH ;;
23 | esac
24 | export PATH
25 | '';
26 | })
27 |
28 | (mkIf config.programs.zsh.enable {
29 | programs.zsh.initExtraFirst = ''
30 | export -U PATH=${sysBin}''${PATH:+:$PATH}
31 | export -U PATH=${usrBin}''${PATH:+:$PATH}
32 | '';
33 | })
34 |
35 | (mkIf config.programs.fish.enable {
36 | programs.fish.loginShellInit = ''
37 | fish_add_path --prepend --global ${sysBin} ${usrBin}
38 | '';
39 | })
40 | ];
41 | }
42 |
--------------------------------------------------------------------------------
/nix/modules/intel-overlay.nix:
--------------------------------------------------------------------------------
1 | {inputs, ...}: {
2 | nixpkgs.overlays = [
3 | (import ./../overlays/intel-pkgs.nix inputs.nixpkgs.legacyPacakges.x86_64-darwin)
4 | ];
5 | }
6 |
--------------------------------------------------------------------------------
/nix/modules/mk-host.nix:
--------------------------------------------------------------------------------
1 | {
2 | hostName,
3 | hostModules,
4 | }: {
5 | config,
6 | lib,
7 | pkgs,
8 | flake,
9 | ...
10 | }: {
11 | imports = hostModules;
12 |
13 | config = {
14 | _module.args.hostName = hostName;
15 | services.nix-daemon.enable = true;
16 | nix.extraOptions = builtins.readFile "${flake}/nix.conf";
17 | };
18 | }
19 |
--------------------------------------------------------------------------------
/nix/modules/mk-user.nix:
--------------------------------------------------------------------------------
1 | {
2 | userName,
3 | userHome,
4 | userModules,
5 | }: {
6 | config,
7 | inputs,
8 | ...
9 | } @ args: {
10 | users.users."${userName}".home = userHome;
11 | home-manager.users."${userName}" = {
12 | imports =
13 | [
14 | {
15 | config._module.args = {
16 | system-config = config;
17 | inherit inputs;
18 | };
19 | }
20 | ]
21 | ++ userModules;
22 | config = {
23 | home.stateVersion = "22.05";
24 |
25 | home.file.".config/nix/nix.conf".text = ''
26 | experimental-features = nix-command flakes
27 | extra-platforms = aarch64-darwin x86_64-darwin
28 | '';
29 | };
30 | };
31 | }
32 |
--------------------------------------------------------------------------------
/nix/modules/niv-managed-dmg-overlay.nix:
--------------------------------------------------------------------------------
1 | {
2 | config,
3 | pkgs,
4 | inputs,
5 | ...
6 | }: let
7 | inherit (inputs) nivSources;
8 | hm = pkgs.callPackage "${inputs.home-manager}/modules/files.nix" {
9 | lib = pkgs.lib // inputs.home-manager.lib;
10 | };
11 | hdiutil = hm.config.lib.file.mkOutOfStoreSymlink "/usr/bin/hdiutil";
12 | in {
13 | nixpkgs.overlays = [
14 | (import ./../overlays/niv-managed-dmg-apps.nix {
15 | inherit nivSources hdiutil;
16 | })
17 | ];
18 | }
19 |
--------------------------------------------------------------------------------
/nix/overlays/darwin-rebuild.nix:
--------------------------------------------------------------------------------
1 | # A wrapper over darwin-rebuild that uses this flake.
2 | darwin: flake: self: super: let
3 | inherit (self.pkgs) writeScriptBin;
4 | darwin-rebuild = writeScriptBin "darwin-rebuild" ''
5 | if [ -z "$*" ]; then
6 | exec ${darwin.profile}/sw/bin/darwin-rebuild --flake ${flake} switch
7 | else
8 | exec ${darwin.profile}/sw/bin/darwin-rebuild --flake ${flake} "''${@}"
9 | fi
10 | '';
11 | in {
12 | inherit darwin-rebuild;
13 | }
14 |
--------------------------------------------------------------------------------
/nix/overlays/intel-pkgs.nix:
--------------------------------------------------------------------------------
1 | # This is used to fallback some packages that are broken or
2 | # will never be available on M1. NOTE: Using packages here
3 | # will most likely require you to have Rosetta-2 installed.
4 | intelPkgs: self: super: let
5 | inherit (super) stdenv lib;
6 | isMacM1 = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64;
7 | intelOverrides = {
8 | inherit
9 | (intelPkgs)
10 | llvmPackages_6
11 | llvmPackages_7
12 | llvmPackages_8
13 | llvmPackages_9
14 | llvmPackages_10
15 | ;
16 | };
17 | in
18 | if isMacM1
19 | then intelOverrides
20 | else {}
21 |
--------------------------------------------------------------------------------
/nix/overlays/niv-managed-dmg-apps.nix:
--------------------------------------------------------------------------------
1 | {
2 | nivSources,
3 | hdiutil,
4 | }: self: super: let
5 | inherit (self) stdenvNoCC lib;
6 | inherit (lib) isAttrs mapAttrs filterAttrs hasInfix;
7 | nivManagedDmgs = filterAttrs (n: v: (isAttrs v) && (hasInfix ".dmg" v.url)) nivSources;
8 | dmgInstall = args:
9 | stdenvNoCC.mkDerivation (args
10 | // {
11 | sourceRoot = ".";
12 | preferLocalBuild = true;
13 | phases = ["installPhase"];
14 | installPhase = ''
15 | mkdir -p mnt $out/Applications
16 | ${hdiutil} attach -readonly -mountpoint mnt $src
17 | cp -r mnt/*.app $out/Applications/
18 | ${hdiutil} detach -force mnt
19 | '';
20 | });
21 | dmgPkgs = mapAttrs (n: v:
22 | dmgInstall {
23 | src = v;
24 | name = n;
25 | version = v.version;
26 | })
27 | nivManagedDmgs;
28 | in {
29 | nivApps = dmgPkgs;
30 | }
31 |
--------------------------------------------------------------------------------
/templates/minimal/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | *.log
3 | tmp/
4 |
5 | result*
6 |
--------------------------------------------------------------------------------
/templates/minimal/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: help
2 | help:
3 | @cat $(MAKEFILE_LIST) | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
4 |
5 | .PHONY: bootstrap
6 | bootstrap: ## Activate system on fresh MacOS.
7 | env NIX_CONF_DIR="$(CURDIR)" nix run
8 |
9 | .PHONY: update
10 | update: ## Update flake inputs.
11 | nix flake update
12 |
13 | .PHONY: install
14 | install: ## Activate system
15 | nix run
16 |
17 | .PHONY: test
18 | test: ## Test system is installable.
19 | nix flake check
20 | nix run '.#format' -- --check .
21 |
22 | .PHONY: format
23 | format: ## Format all nix files.
24 | nix run '.#format' -- --quiet .
25 |
--------------------------------------------------------------------------------
/templates/minimal/README.md:
--------------------------------------------------------------------------------
1 | ## Bootstrap.
2 |
3 | Edit `flake.nix` and change `hostName` and `userName`.
4 |
5 | On a fresh MacOS install you can run the following to bootstrap your system.
6 | ``` sh
7 | env NIX_CONF_DIR=$PWD nix run
8 | ```
9 |
10 | ## Test your system is buildable.
11 |
12 | ``` sh
13 | nix flake check
14 | ```
15 |
16 | ## Activate your system
17 |
18 | ``` sh
19 | nix run
20 | ```
21 |
22 | ## Interactive shell
23 |
24 | Your flake includes a development shell which includes tools
25 | like `niv` (for dependency management), `alejandra` (nix code formatter).
26 |
27 | ``` sh
28 | nix develop
29 | ```
30 |
31 | ## Add home-managed nixpkgs applications.
32 |
33 | Edit `nix/homeConfigurations/your-username.nix` and add
34 | the desired values for `home.programs`, for example, if
35 | you like to use [exa](https://the.exa.website/):
36 |
37 | ``` nix
38 | home.programs = with pkgs; [ exa ];
39 | ```
40 |
41 |
42 | ## Install home-managed dmg applications (sourced by Niv)
43 |
44 | Your flake can use [niv](https://github.com/nmattia/niv) to declare
45 | external source dependencies, like `.dmg` applications distributed via
46 | github releases or for MacOS apps not available via `nixpkgs`.
47 |
48 | See `niv --help` and the [niv homepage](https://github.com/nmattia/niv) for
49 | more instructions on adding, updating and removing dependencies.
50 |
51 | Suppose we would like to install [Keytty](https://keytty.com/).
52 | First, we need to determine the `.dmg` url from [keytty's releases](https://github.com/keytty/shelter/releases) page.
53 | And then add a dependency using `niv`.
54 |
55 | ``` sh
56 | niv add Keytty -t 'https://github.com/keytty/shelter/releases/download//Keytty..dmg' -v '1.2.8'
57 | ```
58 |
59 | After adding the `Keytty` dependency at version `1.2.8`, we can install it
60 | by editing `nix/homeConfigurations/your-username.nix`:
61 |
62 | ``` nix
63 | home.programs = [ pkgs.nivApps.Keytty ] # NOTE: same name as niv-managed dependency.
64 | ```
65 |
--------------------------------------------------------------------------------
/templates/minimal/flake.nix:
--------------------------------------------------------------------------------
1 | {
2 | inputs = {
3 | # change tag or commit of nixpkgs for your system
4 | nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
5 |
6 | # change main to a tag o git revision
7 | mk-darwin-system.url = "github:vic/mk-darwin-system/main";
8 | mk-darwin-system.inputs.nixpkgs.follows = "nixpkgs";
9 | };
10 |
11 | outputs = {mk-darwin-system, ...} @ inputs: let
12 | userName = "your-username";
13 | hostName = "your-hostname";
14 |
15 | darwinFlake = mk-darwin-system.mkFlake {
16 | inherit userName hostName inputs;
17 | flake = ./.;
18 | };
19 | in
20 | darwinFlake;
21 | }
22 |
--------------------------------------------------------------------------------
/templates/minimal/nix.conf:
--------------------------------------------------------------------------------
1 | system = aarch64-darwin
2 | extra-platforms = aarch64-darwin x86_64-darwin
3 | experimental-features = nix-command flakes
4 | build-users-group = nixbld
5 |
--------------------------------------------------------------------------------
/templates/minimal/nix/homeConfigurations/your-username.nix:
--------------------------------------------------------------------------------
1 | {
2 | config,
3 | lib,
4 | pkgs,
5 | ...
6 | }:
7 | # for configurable home-manager modules see:
8 | # https://github.com/nix-community/home-manager/blob/master/modules/modules.nix
9 | {
10 | home.packages = with pkgs; [
11 | # Install nixpkgs
12 | exa
13 |
14 | # Install dmg applications versioned by niv from your `nix/sources.json` file.
15 | # See `nix develop -c niv show` on the root of your flake.
16 | nivApps.keytty-dmg
17 | ];
18 |
19 | # enable at least one shell. as for any other program, see customizable options at:
20 | # https://github.com/nix-community/home-manager/blob/master/modules/programs/.nix
21 | programs.fish.enable = true;
22 | }
23 |
--------------------------------------------------------------------------------
/templates/minimal/nix/hostConfigurations/your-hostname.nix:
--------------------------------------------------------------------------------
1 | {
2 | config,
3 | lib,
4 | pkgs,
5 | ...
6 | }:
7 | # for configurable nixos modules see (note that many of them might be linux-only):
8 | # https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/module-list.nix
9 | #
10 | # for configurable nix-darwin modules see
11 | # https://github.com/LnL7/nix-darwin/blob/master/modules/module-list.nix
12 | {
13 | environment.systemPackages = with pkgs; [nixVersions.stable];
14 | }
15 |
--------------------------------------------------------------------------------
/templates/minimal/nix/sources.json:
--------------------------------------------------------------------------------
1 | {
2 | "keytty-dmg": {
3 | "sha256": "08sifc1pjmbbvxryy1h9wda7f36y0sy67spdbgps3f1jdn84pcpn",
4 | "type": "file",
5 | "url": "https://github.com/keytty/shelter/releases/download/1.2.8/Keytty.1.2.8.dmg",
6 | "url_template": "https://github.com/keytty/shelter/releases/download//Keytty..dmg",
7 | "version": "1.2.8"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/templates/minimal/nix/sources.nix:
--------------------------------------------------------------------------------
1 | # This file has been generated by Niv.
2 | let
3 | #
4 | # The fetchers. fetch_ fetches specs of type .
5 | #
6 | fetch_file = pkgs: name: spec: let
7 | name' = sanitizeName name + "-src";
8 | in
9 | if spec.builtin or true
10 | then
11 | builtins_fetchurl {
12 | inherit (spec) url sha256;
13 | name = name';
14 | }
15 | else
16 | pkgs.fetchurl {
17 | inherit (spec) url sha256;
18 | name = name';
19 | };
20 |
21 | fetch_tarball = pkgs: name: spec: let
22 | name' = sanitizeName name + "-src";
23 | in
24 | if spec.builtin or true
25 | then
26 | builtins_fetchTarball {
27 | name = name';
28 | inherit (spec) url sha256;
29 | }
30 | else
31 | pkgs.fetchzip {
32 | name = name';
33 | inherit (spec) url sha256;
34 | };
35 |
36 | fetch_git = name: spec: let
37 | ref =
38 | if spec ? ref
39 | then spec.ref
40 | else if spec ? branch
41 | then "refs/heads/${spec.branch}"
42 | else if spec ? tag
43 | then "refs/tags/${spec.tag}"
44 | else abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
45 | submodules =
46 | if spec ? submodules
47 | then spec.submodules
48 | else false;
49 | submoduleArg = let
50 | nixSupportsSubmodules = builtins.compareVersions builtins.nixVersion "2.4" >= 0;
51 | emptyArgWithWarning =
52 | if submodules == true
53 | then
54 | builtins.trace
55 | (
56 | "The niv input \"${name}\" uses submodules "
57 | + "but your nix's (${builtins.nixVersion}) builtins.fetchGit "
58 | + "does not support them"
59 | )
60 | {}
61 | else {};
62 | in
63 | if nixSupportsSubmodules
64 | then {inherit submodules;}
65 | else emptyArgWithWarning;
66 | in
67 | builtins.fetchGit
68 | ({
69 | url = spec.repo;
70 | inherit (spec) rev;
71 | inherit ref;
72 | }
73 | // submoduleArg);
74 |
75 | fetch_local = spec: spec.path;
76 |
77 | fetch_builtin-tarball = name:
78 | throw
79 | '' [${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
80 | $ niv modify ${name} -a type=tarball -a builtin=true'';
81 |
82 | fetch_builtin-url = name:
83 | throw
84 | '' [${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
85 | $ niv modify ${name} -a type=file -a builtin=true'';
86 |
87 | #
88 | # Various helpers
89 | #
90 |
91 | # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
92 | sanitizeName = name: (
93 | concatMapStrings (s:
94 | if builtins.isList s
95 | then "-"
96 | else s)
97 | (
98 | builtins.split "[^[:alnum:]+._?=-]+"
99 | ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
100 | )
101 | );
102 |
103 | # The set of packages used when specs are fetched using non-builtins.
104 | mkPkgs = sources: system: let
105 | sourcesNixpkgs =
106 | import (builtins_fetchTarball {inherit (sources.nixpkgs) url sha256;}) {inherit system;};
107 | hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
108 | hasThisAsNixpkgsPath = == ./.;
109 | in
110 | if builtins.hasAttr "nixpkgs" sources
111 | then sourcesNixpkgs
112 | else if hasNixpkgsPath && ! hasThisAsNixpkgsPath
113 | then import {}
114 | else
115 | abort
116 | ''
117 | Please specify either (through -I or NIX_PATH=nixpkgs=...) or
118 | add a package called "nixpkgs" to your sources.json.
119 | '';
120 |
121 | # The actual fetching function.
122 | fetch = pkgs: name: spec:
123 | if ! builtins.hasAttr "type" spec
124 | then abort "ERROR: niv spec ${name} does not have a 'type' attribute"
125 | else if spec.type == "file"
126 | then fetch_file pkgs name spec
127 | else if spec.type == "tarball"
128 | then fetch_tarball pkgs name spec
129 | else if spec.type == "git"
130 | then fetch_git name spec
131 | else if spec.type == "local"
132 | then fetch_local spec
133 | else if spec.type == "builtin-tarball"
134 | then fetch_builtin-tarball name
135 | else if spec.type == "builtin-url"
136 | then fetch_builtin-url name
137 | else abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
138 |
139 | # If the environment variable NIV_OVERRIDE_${name} is set, then use
140 | # the path directly as opposed to the fetched source.
141 | replace = name: drv: let
142 | saneName = stringAsChars (c:
143 | if isNull (builtins.match "[a-zA-Z0-9]" c)
144 | then "_"
145 | else c)
146 | name;
147 | ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
148 | in
149 | if ersatz == ""
150 | then drv
151 | else
152 | # this turns the string into an actual Nix path (for both absolute and
153 | # relative paths)
154 | if builtins.substring 0 1 ersatz == "/"
155 | then /. + ersatz
156 | else /. + builtins.getEnv "PWD" + "/${ersatz}";
157 |
158 | # Ports of functions for older nix versions
159 |
160 | # a Nix version of mapAttrs if the built-in doesn't exist
161 | mapAttrs =
162 | builtins.mapAttrs
163 | or (
164 | f: set:
165 | with builtins;
166 | listToAttrs (map (attr: {
167 | name = attr;
168 | value = f attr set.${attr};
169 | }) (attrNames set))
170 | );
171 |
172 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
173 | range = first: last:
174 | if first > last
175 | then []
176 | else builtins.genList (n: first + n) (last - first + 1);
177 |
178 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
179 | stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
180 |
181 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
182 | stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
183 | concatMapStrings = f: list: concatStrings (map f list);
184 | concatStrings = builtins.concatStringsSep "";
185 |
186 | # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
187 | optionalAttrs = cond: as:
188 | if cond
189 | then as
190 | else {};
191 |
192 | # fetchTarball version that is compatible between all the versions of Nix
193 | builtins_fetchTarball = {
194 | url,
195 | name ? null,
196 | sha256,
197 | } @ attrs: let
198 | inherit (builtins) lessThan nixVersion fetchTarball;
199 | in
200 | if lessThan nixVersion "1.12"
201 | then fetchTarball ({inherit url;} // (optionalAttrs (!isNull name) {inherit name;}))
202 | else fetchTarball attrs;
203 |
204 | # fetchurl version that is compatible between all the versions of Nix
205 | builtins_fetchurl = {
206 | url,
207 | name ? null,
208 | sha256,
209 | } @ attrs: let
210 | inherit (builtins) lessThan nixVersion fetchurl;
211 | in
212 | if lessThan nixVersion "1.12"
213 | then fetchurl ({inherit url;} // (optionalAttrs (!isNull name) {inherit name;}))
214 | else fetchurl attrs;
215 |
216 | # Create the final "sources" from the config
217 | mkSources = config:
218 | mapAttrs (
219 | name: spec:
220 | if builtins.hasAttr "outPath" spec
221 | then
222 | abort
223 | "The values in sources.json should not have an 'outPath' attribute"
224 | else spec // {outPath = replace name (fetch config.pkgs name spec);}
225 | )
226 | config.sources;
227 |
228 | # The "config" used by the fetchers
229 | mkConfig = {
230 | sourcesFile ?
231 | if builtins.pathExists ./sources.json
232 | then ./sources.json
233 | else null,
234 | sources ?
235 | if isNull sourcesFile
236 | then {}
237 | else builtins.fromJSON (builtins.readFile sourcesFile),
238 | system ? builtins.currentSystem,
239 | pkgs ? mkPkgs sources system,
240 | }: rec {
241 | # The sources, i.e. the attribute set of spec name to spec
242 | inherit sources;
243 |
244 | # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
245 | inherit pkgs;
246 | };
247 | in
248 | mkSources (mkConfig {}) // {__functor = _: settings: mkSources (mkConfig settings);}
249 |
--------------------------------------------------------------------------------