From 57c144515a59efde1dd59078e280a82b32626311 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 03:12:30 +1100 Subject: [PATCH 01/12] system: always add /run to /etc/synthetic.conf on macOS 10.15 onwards Currently if nix-darwin is uninstalled then reinstalled without rebooting, then the `/run` symlink will still remain and nix-darwin won't readd `run` to `/etc/synthetic.conf` meaning the system will be broken on next reboot. --- modules/system/base.nix | 73 +++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/modules/system/base.nix b/modules/system/base.nix index 43c9d7ff..f20e2b64 100644 --- a/modules/system/base.nix +++ b/modules/system/base.nix @@ -2,46 +2,49 @@ { system.activationScripts.createRun.text = '' - if [[ ! -L /run ]]; then - # This file doesn't exist by default on macOS and is only supported after 10.15 - # however every system with Nix installed should have this file otherwise `/nix` - # wouldn't exist. - if [[ -e /etc/synthetic.conf ]]; then - if ! grep -q '^run\b' /etc/synthetic.conf 2>/dev/null; then - echo "setting up /run via /etc/synthetic.conf..." - printf 'run\tprivate/var/run\n' | sudo tee -a /etc/synthetic.conf >/dev/null - fi + IFS="." read -r -a macOSVersion <<< "$(sw_vers -productVersion)" - # for Catalina (10.15) - sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B &>/dev/null || true - # for Big Sur (11.0) - sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t &>/dev/null || true + if [[ ''${macOSVersion[0]} -gt 10 || ( ''${macOSVersion[0]} -eq 10 && ''${macOSVersion[1]} -ge 15 ) ]]; then + if ! grep -q '^run\b' /etc/synthetic.conf 2>/dev/null; then + echo "setting up /run via /etc/synthetic.conf..." + printf 'run\tprivate/var/run\n' | sudo tee -a /etc/synthetic.conf >/dev/null + fi - if [[ ! -L /run ]]; then - printf >&2 'error: apfs.util failed to symlink /run, aborting activation\n' - printf >&2 'To create a symlink from /run to /var/run, please run:\n' - printf >&2 '\n' - printf >&2 "$ printf 'run\tprivate/var/run\n' | sudo tee -a /etc/synthetic.conf" - printf >&2 '$ sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B # For Catalina\n' - printf >&2 '$ sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t # For Big Sur and later\n' >&2 - printf >&2 '\n' - printf >&2 'The current contents of /etc/synthetic.conf is:\n' - printf >&2 '\n' - sudo sed 's/^/ /' /etc/synthetic.conf >&2 - printf >&2 '\n' - exit 1 - fi + if [[ ''${macOSVersion[0]} -gt 10 ]]; then + sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t || true else - echo "setting up /run..." - sudo ln -sfn private/var/run /run + sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B || true + fi - if [[ ! -L /run ]]; then - printf >&2 'error: failed to symlink /run, aborting activation\n' - printf >&2 'To create a symlink from /run to /var/run, please run:\n' - printf >&2 '\n' - printf >&2 '$ sudo ln -sfn private/var/link /run\n' - exit 1 + if [[ ! -L /run ]]; then + printf >&2 'error: apfs.util failed to symlink /run, aborting activation\n' + printf >&2 'To create a symlink from /run to /var/run, please run:\n' + printf >&2 '\n' + printf >&2 "$ printf 'run\tprivate/var/run\n' | sudo tee -a /etc/synthetic.conf" + + if [[ ''${macOSVersion[0]} -gt 10 ]]; then + printf >&2 '$ sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t\n' + else + printf >&2 '$ sudo /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B\n' fi + + printf >&2 '\n' + printf >&2 'The current contents of /etc/synthetic.conf is:\n' + printf >&2 '\n' + sudo sed 's/^/ /' /etc/synthetic.conf >&2 + printf >&2 '\n' + exit 1 + fi + else + echo "setting up /run..." + sudo ln -sfn private/var/run /run + + if [[ ! -L /run ]]; then + printf >&2 'error: failed to symlink /run, aborting activation\n' + printf >&2 'To create a symlink from /run to /var/run, please run:\n' + printf >&2 '\n' + printf >&2 '$ sudo ln -sfn private/var/link /run\n' + exit 1 fi fi ''; From 5cc3c00f9b689fa98c524674ddf5a569005e4bd9 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 02:16:56 +1100 Subject: [PATCH 02/12] readme: move sections under new Channels section --- README.md | 109 +++++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 58bde794..3bbaf5cb 100644 --- a/README.md +++ b/README.md @@ -18,60 +18,14 @@ As the official Nix installer does not include an automated uninstaller, and man - The [Nix installer from Determinate Systems](https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#determinate-nix-installer) is only recommended for use with flake-based setups. **Make sure you use it without the `--determinate` flag**. The `--determinate` flag installs the Determinate Nix distribution which does not work out of the box with nix-darwin. * The [Lix installer](https://lix.systems/install/#on-any-other-linuxmacos-system) supports both flake-based and channel-based setups. -## Installing -If you wish to use nix-darwin with flakes, please refer to the [flakes](#flakes) section. -```bash -nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer -./result/bin/darwin-installer -``` +## Getting started -## Updating +Despite being an experimental feature in Nix currently, nix-darwin recommends that beginners use flakes to manage their nix-darwin configurations. -The installer will configure a channel for this repository. - -```bash -nix-channel --update darwin -darwin-rebuild changelog -``` - -> NOTE: If you are using Nix as a daemon service the channel for that will be owned by root. -> Use `sudo -i nix-channel --update darwin` instead. - -## Uninstalling - -To run the latest version of the uninstaller, you can run the following command: - -``` -nix --extra-experimental-features "nix-command flakes" run nix-darwin#darwin-uninstaller -``` - -If that command doesn't work for you, you can try the locally installed uninstaller: - -``` -darwin-uninstaller -``` - -## Example configuration - -Configuration lives in `~/.nixpkgs/darwin-configuration.nix`. Check out -[modules/examples](https://github.com/LnL7/nix-darwin/tree/master/modules/examples) for some example configurations. - -```nix -{ pkgs, ... }: -{ - # List packages installed in system profile. To search by name, run: - # $ nix-env -qaP | grep wget - environment.systemPackages = - [ pkgs.vim - ]; -} -``` - -## Flakes - -nix-darwin aims for both non-flake and flake configurations to be well supported despite flakes being an experimental feature in Nix. +
+Flakes (Recommended for beginners) ### Step 1. Creating `flake.nix` @@ -156,6 +110,47 @@ nix-darwin.lib.darwinSystem { { pkgs, lib, inputs }: # inputs.self, inputs.nix-darwin, and inputs.nixpkgs can be accessed here ``` +
+ +
+Channels + +### Installing + +```bash +nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer +./result/bin/darwin-installer +``` + +### Updating + +The installer will configure a channel for this repository. + +```bash +nix-channel --update darwin +darwin-rebuild changelog +``` + +> NOTE: If you are using Nix as a daemon service the channel for that will be owned by root. +> Use `sudo -i nix-channel --update darwin` instead. + +### Example configuration + +Configuration lives in `~/.nixpkgs/darwin-configuration.nix`. Check out +[modules/examples](https://github.com/LnL7/nix-darwin/tree/master/modules/examples) for some example configurations. + +```nix +{ pkgs, ... }: +{ + # List packages installed in system profile. To search by name, run: + # $ nix-env -qaP | grep wget + environment.systemPackages = + [ pkgs.vim + ]; +} +``` + +
## Documentation @@ -185,6 +180,20 @@ Whether to enable Linux builder. There's also a small wiki https://github.com/LnL7/nix-darwin/wiki about specific topics, like macOS upgrades. +## Uninstalling + +To run the latest version of the uninstaller, you can run the following command: + +``` +nix --extra-experimental-features "nix-command flakes" run nix-darwin#darwin-uninstaller +``` + +If that command doesn't work for you, you can try the locally installed uninstaller: + +``` +darwin-uninstaller +``` + ## Tests There are basic tests that run sanity checks for some of the modules, From 9a1bea70d5728a19ee0a090dc0bcdeb73f09b7a4 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 02:33:37 +1100 Subject: [PATCH 03/12] installer: move creating default configuration to README --- .github/workflows/test.yml | 20 ++++++++------------ README.md | 25 ++++++------------------- modules/examples/simple.nix | 5 ++--- pkgs/darwin-installer/default.nix | 25 +++---------------------- 4 files changed, 19 insertions(+), 56 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 92d06d4d..90bd6fdd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,17 +49,15 @@ jobs: run: | export NIX_PATH=$HOME/.nix-defexpr/channels - # We run nix-darwin twice to test that it can create darwin-configuration correctly for us - # but we expect it to fail setting up /etc/nix/nix.conf - nix-shell -A installer || true + mkdir -p ~/.config/nix-darwin + cp modules/examples/simple.nix ~/.config/nix-darwin/configuration.nix nixConfHash=$(shasum -a 256 /etc/nix/nix.conf | cut -d ' ' -f 1) /usr/bin/sed -i.bak \ "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \ - ~/.nixpkgs/darwin-configuration.nix + ~/.config/nix-darwin/configuration.nix - nix-shell -A installer - nix-shell -A installer.check + nix-shell -A installer -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - name: Build and activate default derivation run: | . /etc/static/bashrc @@ -97,17 +95,15 @@ jobs: run: | export NIX_PATH=$HOME/.nix-defexpr/channels - # We run nix-darwin twice to test that it can create darwin-configuration correctly for us - # but we expect it to fail setting up /etc/nix/nix.conf - nix-shell -A installer || true + mkdir -p ~/.config/nix-darwin + cp modules/examples/simple.nix ~/.config/nix-darwin/configuration.nix nixConfHash=$(shasum -a 256 /etc/nix/nix.conf | cut -d ' ' -f 1) /usr/bin/sed -i.bak \ "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \ - ~/.nixpkgs/darwin-configuration.nix + ~/.config/nix-darwin/configuration.nix - nix-shell -A installer - nix-shell -A installer.check + nix-shell -A installer -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - name: Build and activate default derivation run: | . /etc/static/bashrc diff --git a/README.md b/README.md index 3bbaf5cb..bfc825e9 100644 --- a/README.md +++ b/README.md @@ -115,14 +115,18 @@ nix-darwin.lib.darwinSystem {
Channels -### Installing +### Step 1. Creating `configuration.nix` + +Copy the [simple](./modules/examples/simple.nix) example to `~/.config/nix-darwin/configuration.nix`. + +### Step 2. Installing `nix-darwin` ```bash nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer ./result/bin/darwin-installer ``` -### Updating +### Step 3. Updating `nix-darwin` The installer will configure a channel for this repository. @@ -133,23 +137,6 @@ darwin-rebuild changelog > NOTE: If you are using Nix as a daemon service the channel for that will be owned by root. > Use `sudo -i nix-channel --update darwin` instead. - -### Example configuration - -Configuration lives in `~/.nixpkgs/darwin-configuration.nix`. Check out -[modules/examples](https://github.com/LnL7/nix-darwin/tree/master/modules/examples) for some example configurations. - -```nix -{ pkgs, ... }: -{ - # List packages installed in system profile. To search by name, run: - # $ nix-env -qaP | grep wget - environment.systemPackages = - [ pkgs.vim - ]; -} -``` -
## Documentation diff --git a/modules/examples/simple.nix b/modules/examples/simple.nix index 8d769a2c..5771ec60 100644 --- a/modules/examples/simple.nix +++ b/modules/examples/simple.nix @@ -7,9 +7,8 @@ [ pkgs.vim ]; - # Use a custom configuration.nix location. - # $ darwin-rebuild switch -I darwin-config=$HOME/.config/nixpkgs/darwin/configuration.nix - # environment.darwinConfig = "$HOME/.config/nixpkgs/darwin/configuration.nix"; + # Use custom location for configuration.nix. + environment.darwinConfig = "$HOME/.config/nix-darwin/configuration.nix"; # Enable alternative shell support in nix-darwin. # programs.fish.enable = true; diff --git a/pkgs/darwin-installer/default.nix b/pkgs/darwin-installer/default.nix index 78ca3c88..0eed5187 100644 --- a/pkgs/darwin-installer/default.nix +++ b/pkgs/darwin-installer/default.nix @@ -47,25 +47,6 @@ stdenv.mkDerivation { echo >&2 "Installing nix-darwin..." echo >&2 - config="$HOME/.nixpkgs/darwin-configuration.nix" - if ! test -f "$config"; then - echo "copying example configuration.nix" >&2 - mkdir -p "$HOME/.nixpkgs" - cp "${../../modules/examples/simple.nix}" "$config" - chmod u+w "$config" - fi - - # Skip when stdin is not a tty, eg. - # $ yes | darwin-installer - if test -t 0; then - read -p "Would you like to edit the default configuration.nix before starting? [y/N] " i - case "$i" in - y|Y) - PATH=$_PATH ''${EDITOR:-nano} "$config" - ;; - esac - fi - i=y darwinPath=$(NIX_PATH=$HOME/.nix-defexpr/channels nix-instantiate --eval -E '' 2> /dev/null) || true if ! test -e "$darwinPath"; then @@ -81,13 +62,13 @@ stdenv.mkDerivation { fi export NIX_PATH=${nixPath} - system=$(nix-build '' -I "darwin-config=$config" -A system --no-out-link --show-trace) + system=$(nix-build '' -A system --no-out-link --show-trace) export PATH=$system/sw/bin:$PATH - darwin-rebuild "$action" -I "darwin-config=$config" + darwin-rebuild "$action" echo >&2 - echo >&2 " Open '$config' to get started." + echo >&2 " Installation complete." echo >&2 " See the README for more information: https://github.com/LnL7/nix-darwin/blob/master/README.md" echo >&2 echo >&2 " Please log out and log in again to make sure nix-darwin is properly loaded." From 65ea368ebbed4fa52a1a59fcb06848c49b310c9c Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 02:51:42 +1100 Subject: [PATCH 04/12] installer: move channel creation to README --- .github/workflows/test.yml | 6 ++++-- README.md | 16 +++++++++------- pkgs/darwin-installer/default.nix | 14 -------------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 90bd6fdd..73a3fb71 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -41,8 +41,9 @@ jobs: with: install_url: https://releases.nixos.org/nix/nix-2.18.8/install nix_path: nixpkgs=channel:${{ env.CURRENT_STABLE_CHANNEL }} - - name: Install ${{ env.CURRENT_STABLE_CHANNEL }} channel + - name: Install channels run: | + nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin nix-channel --add https://nixos.org/channels/${{ env.CURRENT_STABLE_CHANNEL }} nixpkgs nix-channel --update - name: Install nix-darwin and test @@ -87,8 +88,9 @@ jobs: with: install_url: https://releases.nixos.org/nix/nix-2.24.9/install nix_path: nixpkgs=channel:nixpkgs-unstable - - name: Install nixpkgs-unstable channel + - name: Install channels run: | + nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs nix-channel --update - name: Install nix-darwin and test diff --git a/README.md b/README.md index bfc825e9..f029c9a5 100644 --- a/README.md +++ b/README.md @@ -119,24 +119,26 @@ nix-darwin.lib.darwinSystem { Copy the [simple](./modules/examples/simple.nix) example to `~/.config/nix-darwin/configuration.nix`. -### Step 2. Installing `nix-darwin` +### Step 2. Adding `nix-darwin` channel + +```bash +nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin +nix-channel --update +``` + +### Step 3. Installing `nix-darwin` ```bash nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer ./result/bin/darwin-installer ``` -### Step 3. Updating `nix-darwin` - -The installer will configure a channel for this repository. +### Step 4. Updating `nix-darwin` ```bash nix-channel --update darwin darwin-rebuild changelog ``` - -> NOTE: If you are using Nix as a daemon service the channel for that will be owned by root. -> Use `sudo -i nix-channel --update darwin` instead. ## Documentation diff --git a/pkgs/darwin-installer/default.nix b/pkgs/darwin-installer/default.nix index 0eed5187..630c5bfe 100644 --- a/pkgs/darwin-installer/default.nix +++ b/pkgs/darwin-installer/default.nix @@ -47,20 +47,6 @@ stdenv.mkDerivation { echo >&2 "Installing nix-darwin..." echo >&2 - i=y - darwinPath=$(NIX_PATH=$HOME/.nix-defexpr/channels nix-instantiate --eval -E '' 2> /dev/null) || true - if ! test -e "$darwinPath"; then - if test -t 0; then - read -p "Would you like to manage with nix-channel? [y/N] " i - fi - case "$i" in - y|Y) - nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin - nix-channel --update - ;; - esac - fi - export NIX_PATH=${nixPath} system=$(nix-build '' -A system --no-out-link --show-trace) From 62f9402af0a5412045385b8ba4cb79bd8880a2c3 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 12:33:06 +1100 Subject: [PATCH 05/12] readme: add using `nix-darwin` section for non-flakes --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f029c9a5..a4ce5986 100644 --- a/README.md +++ b/README.md @@ -133,11 +133,20 @@ nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer ./result/bin/darwin-installer ``` -### Step 4. Updating `nix-darwin` +### Step 4. Using `nix-darwin` + +After installing, you can run `darwin-rebuild` to apply changes to your system: + +```bash +darwin-rebuild switch +``` + +### Step 5. Updating `nix-darwin` + +You can update `nix-darwin` using the following command: ```bash nix-channel --update darwin -darwin-rebuild changelog ``` From 60ed03d0b10fa88054a73a0d34338f03f8d73f53 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 02:14:06 +1100 Subject: [PATCH 06/12] installer: remove --- .github/workflows/test.yml | 12 ++-- README.md | 7 +- default.nix | 17 +---- pkgs/darwin-installer/default.nix | 109 ------------------------------ 4 files changed, 14 insertions(+), 131 deletions(-) delete mode 100644 pkgs/darwin-installer/default.nix diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 73a3fb71..43aed103 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,7 +46,7 @@ jobs: nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin nix-channel --add https://nixos.org/channels/${{ env.CURRENT_STABLE_CHANNEL }} nixpkgs nix-channel --update - - name: Install nix-darwin and test + - name: Install nix-darwin run: | export NIX_PATH=$HOME/.nix-defexpr/channels @@ -58,7 +58,9 @@ jobs: "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \ ~/.config/nix-darwin/configuration.nix - nix-shell -A installer -I darwin-config=$HOME/.config/nix-darwin/configuration.nix + nix run .#darwin-rebuild \ + -- switch \ + -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - name: Build and activate default derivation run: | . /etc/static/bashrc @@ -93,7 +95,7 @@ jobs: nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs nix-channel --update - - name: Install nix-darwin and test + - name: Install nix-darwin run: | export NIX_PATH=$HOME/.nix-defexpr/channels @@ -105,7 +107,9 @@ jobs: "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \ ~/.config/nix-darwin/configuration.nix - nix-shell -A installer -I darwin-config=$HOME/.config/nix-darwin/configuration.nix + nix run .#darwin-rebuild \ + -- switch \ + -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - name: Build and activate default derivation run: | . /etc/static/bashrc diff --git a/README.md b/README.md index a4ce5986..c7fb408f 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Make sure to set `nixpkgs.hostPlatform` in your `configuration.nix` to either `x ### Step 2. Installing `nix-darwin` -Instead of using `darwin-installer`, you can just run `darwin-rebuild switch` to install nix-darwin. As `darwin-rebuild` won't be installed in your `PATH` yet, you can use the following command: +Unlike NixOS, `nix-darwin` does not have an installer, you can just run `darwin-rebuild switch` to install nix-darwin. As `darwin-rebuild` won't be installed in your `PATH` yet, you can use the following command: ```bash nix run nix-darwin -- switch --flake ~/.config/nix-darwin @@ -128,9 +128,10 @@ nix-channel --update ### Step 3. Installing `nix-darwin` +To install `nix-darwin`, you can just run `darwin-rebuild switch` to install nix-darwin. As `darwin-rebuild` won't be installed in your `PATH` yet, you can use the following command: + ```bash -nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer -./result/bin/darwin-installer +nix --extra-experimental-features "nix-command flakes" run nix-darwin -- switch -I darwin-config=$HOME/.config/nix-darwin/configuration.nix ``` ### Step 4. Using `nix-darwin` diff --git a/default.nix b/default.nix index c93b4783..075ae6d2 100644 --- a/default.nix +++ b/default.nix @@ -1,8 +1,8 @@ { nixpkgs ? , configuration ? -, lib ? pkgs.lib -, pkgs ? import nixpkgs { inherit system; } , system ? builtins.currentSystem +, pkgs ? import nixpkgs { inherit system; } +, lib ? pkgs.lib }: let @@ -15,20 +15,7 @@ let nixpkgs.system = lib.mkDefault system; }; }; - - # The source code of this repo needed by the installer. - nix-darwin = lib.cleanSource ( - lib.cleanSourceWith { - # We explicitly specify a name here otherwise `cleanSource` will use the - # basename of ./. which might be different for different clones of this - # repo leading to non-reproducible outputs. - name = "nix-darwin"; - src = ./.; - } - ); in - eval // { - installer = pkgs.callPackage ./pkgs/darwin-installer { inherit nix-darwin; }; uninstaller = pkgs.callPackage ./pkgs/darwin-uninstaller { }; } diff --git a/pkgs/darwin-installer/default.nix b/pkgs/darwin-installer/default.nix deleted file mode 100644 index 630c5bfe..00000000 --- a/pkgs/darwin-installer/default.nix +++ /dev/null @@ -1,109 +0,0 @@ -{ stdenv, nix, pkgs, nix-darwin }: - -let - nixPath = pkgs.lib.concatStringsSep ":" [ - "darwin=${nix-darwin}" - "nixpkgs=${pkgs.path}" - "$HOME/.nix-defexpr/channels" - "/nix/var/nix/profiles/per-user/root/channels" - "$NIX_PATH" - ]; -in - -stdenv.mkDerivation { - name = "darwin-installer"; - preferLocalBuild = true; - - unpackPhase = ":"; - - installPhase = '' - mkdir -p $out/bin - echo "$shellHook" > $out/bin/darwin-installer - chmod +x $out/bin/darwin-installer - ''; - - shellHook = '' - #!${stdenv.shell} - set -e - - _PATH=$PATH - export PATH=/nix/var/nix/profiles/default/bin:${nix}/bin:${pkgs.gnused}/bin:${pkgs.openssh}/bin:/usr/bin:/bin:/usr/sbin:/sbin - - action=switch - while [ "$#" -gt 0 ]; do - i="$1"; shift 1 - case "$i" in - --help) - echo "darwin-installer: [--help] [--check]" - exit - ;; - --check) - action=check - ;; - esac - done - - echo >&2 - echo >&2 "Installing nix-darwin..." - echo >&2 - - export NIX_PATH=${nixPath} - system=$(nix-build '' -A system --no-out-link --show-trace) - - export PATH=$system/sw/bin:$PATH - darwin-rebuild "$action" - - echo >&2 - echo >&2 " Installation complete." - echo >&2 " See the README for more information: https://github.com/LnL7/nix-darwin/blob/master/README.md" - echo >&2 - echo >&2 " Please log out and log in again to make sure nix-darwin is properly loaded." - echo >&2 - exit - ''; - - passthru.check = stdenv.mkDerivation { - name = "run-darwin-test"; - shellHook = '' - set -e - echo >&2 "running installer tests..." - echo >&2 - - echo >&2 "checking configuration.nix" - test -f ~/.nixpkgs/darwin-configuration.nix - test -w ~/.nixpkgs/darwin-configuration.nix - echo >&2 "checking darwin channel" - readlink ~/.nix-defexpr/channels/darwin - test -e ~/.nix-defexpr/channels/darwin - echo >&2 "checking /etc" - readlink /etc/static - test -e /etc/static - echo >&2 "checking profile" - cat /etc/profile - (! grep nix-daemon.sh /etc/profile) - echo >&2 "checking /run/current-system" - readlink /run - test -e /run - readlink /run/current-system - test -e /run/current-system - echo >&2 "checking system profile" - readlink /nix/var/nix/profiles/system - test -e /nix/var/nix/profiles/system - - echo >&2 "checking bash environment" - env -i USER=john HOME=/Users/john bash -li -c 'echo $PATH' - env -i USER=john HOME=/Users/john bash -li -c 'echo $PATH' | grep /Users/john/.nix-profile/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin - env -i USER=john HOME=/Users/john bash -li -c 'echo $NIX_PATH' - env -i USER=john HOME=/Users/john bash -li -c 'echo $NIX_PATH' | grep darwin-config=/Users/john/.nixpkgs/darwin-configuration.nix:/nix/var/nix/profiles/per-user/root/channels - - echo >&2 "checking zsh environment" - env -i USER=john HOME=/Users/john zsh -l -c 'echo $PATH' - env -i USER=john HOME=/Users/john zsh -l -c 'echo $PATH' | grep /Users/john/.nix-profile/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin - env -i USER=john HOME=/Users/john zsh -l -c 'echo $NIX_PATH' - env -i USER=john HOME=/Users/john zsh -l -c 'echo $NIX_PATH' | grep darwin-config=/Users/john/.nixpkgs/darwin-configuration.nix:/nix/var/nix/profiles/per-user/root/channels - - echo >&2 ok - exit - ''; - }; -} From 3c27b0874017b325b0ed7269705c0d2df5df42dc Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 12:46:35 +1100 Subject: [PATCH 07/12] readme: update `Documentation` section --- README.md | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c7fb408f..2623b9fa 100644 --- a/README.md +++ b/README.md @@ -153,31 +153,9 @@ nix-channel --update darwin ## Documentation -Reference documentation of all the options is available [here](https://daiderd.com/nix-darwin/manual/index.html). -This can also be accessed locally using `man 5 configuration.nix`. +`darwin-help` will open up a local copy of the reference documentation, it can also be found online [here](https://daiderd.com/nix-darwin/manual/index.html). -`darwin-help` will open a HTML version of the manpage in the default browser. - -Furthermore there's `darwin-option` to introspect the settings of a system and its available options. -> NOTE: `darwin-option` is only available to non-flake installations. - -``` -$ darwin-option nix.linux-builder.enable -Value: -true - -Default: -false - -Example: -true - -Description: -Whether to enable Linux builder. -``` - -There's also a small wiki https://github.com/LnL7/nix-darwin/wiki about -specific topics, like macOS upgrades. +The documentation is also available as manpages by running `man 5 configuration.nix`. ## Uninstalling From a4d4d12e3885f9fea3100c73d024664ea9572f94 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 13:11:30 +1100 Subject: [PATCH 08/12] examples: change default architecture to `aarch64-darwin` --- .github/workflows/test.yml | 14 ++++++++++++-- modules/examples/flake/flake.nix | 5 +---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 43aed103..b9eb32f5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -149,6 +149,9 @@ jobs: /usr/bin/sed -i.bak \ "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \ flake.nix + /usr/bin/sed -i.bak \ + 's/nixpkgs.hostPlatform = "aarch64-darwin";/nixpkgs.hostPlatform = "'$(nix eval --expr builtins.currentSystem --impure --raw)'";/' \ + flake.nix popd nix run .#darwin-rebuild -- \ switch --flake ~/.config/nix-darwin#simple \ @@ -157,7 +160,9 @@ jobs: - name: Rebuild and activate simple flake, but this time using nix-darwin's flake interface run: | . /etc/static/bashrc - darwin-rebuild build --flake ./modules/examples/flake#simple --override-input nix-darwin . --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} + darwin-rebuild switch --flake ~/.config/nix-darwin#simple \ + --override-input nix-darwin . \ + --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} - name: Test uninstallation of nix-darwin run: | nix run .#darwin-uninstaller --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} @@ -182,6 +187,9 @@ jobs: /usr/bin/sed -i.bak \ "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \ flake.nix + /usr/bin/sed -i.bak \ + 's/nixpkgs.hostPlatform = "aarch64-darwin";/nixpkgs.hostPlatform = "'$(nix eval --expr builtins.currentSystem --impure --raw)'";/' \ + flake.nix popd nix run .#darwin-rebuild -- \ switch --flake ~/.config/nix-darwin#simple \ @@ -190,7 +198,9 @@ jobs: - name: Rebuild and activate simple flake, but this time using nix-darwin's flake interface run: | . /etc/static/bashrc - darwin-rebuild build --flake ./modules/examples/flake#simple --override-input nix-darwin . --override-input nixpkgs nixpkgs/nixpkgs-unstable + darwin-rebuild switch --flake ~/.config/nix-darwin#simple \ + --override-input nix-darwin . \ + --override-input nixpkgs nixpkgs/nixpkgs-unstable - name: Test uninstallation of nix-darwin run: | nix run .#darwin-uninstaller --override-input nixpkgs nixpkgs/nixpkgs-unstable diff --git a/modules/examples/flake/flake.nix b/modules/examples/flake/flake.nix index dbcc420a..4520b8ff 100644 --- a/modules/examples/flake/flake.nix +++ b/modules/examples/flake/flake.nix @@ -30,7 +30,7 @@ system.stateVersion = 5; # The platform the configuration will be used on. - nixpkgs.hostPlatform = "x86_64-darwin"; + nixpkgs.hostPlatform = "aarch64-darwin"; }; in { @@ -39,8 +39,5 @@ darwinConfigurations."simple" = nix-darwin.lib.darwinSystem { modules = [ configuration ]; }; - - # Expose the package set, including overlays, for convenience. - darwinPackages = self.darwinConfigurations."simple".pkgs; }; } From 9fe8a0a7387d24a4e5113587256e37d5f1294486 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 17 Nov 2024 16:16:32 +1100 Subject: [PATCH 09/12] ci: check that switching to new configurations works after installation --- .github/workflows/test.yml | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b9eb32f5..d26e0519 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -61,10 +61,17 @@ jobs: nix run .#darwin-rebuild \ -- switch \ -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - - name: Build and activate default derivation + - name: Switch to new configuration run: | . /etc/static/bashrc + + /usr/bin/sed -i.bak \ + "s/pkgs.vim/pkgs.hello/" \ + ~/.config/nix-darwin/configuration.nix + darwin-rebuild switch -I darwin=. + + hello - name: Test uninstallation of nix-darwin run: | nix run .#darwin-uninstaller \ @@ -110,10 +117,17 @@ jobs: nix run .#darwin-rebuild \ -- switch \ -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - - name: Build and activate default derivation + - name: Switch to new configuration run: | . /etc/static/bashrc + + /usr/bin/sed -i.bak \ + "s/pkgs.vim/pkgs.hello/" \ + ~/.config/nix-darwin/configuration.nix + darwin-rebuild switch -I darwin=. + + hello - name: Test uninstallation of nix-darwin run: | # A regression in Nix 2.19 means we need to put `--extra-experimental-features` before `--override-input` @@ -157,12 +171,19 @@ jobs: switch --flake ~/.config/nix-darwin#simple \ --override-input nix-darwin . \ --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} - - name: Rebuild and activate simple flake, but this time using nix-darwin's flake interface + - name: Switch to new configuration run: | . /etc/static/bashrc + + /usr/bin/sed -i.bak \ + "s/pkgs.vim/pkgs.hello/" \ + ~/.config/nix-darwin/flake.nix + darwin-rebuild switch --flake ~/.config/nix-darwin#simple \ --override-input nix-darwin . \ --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} + + hello - name: Test uninstallation of nix-darwin run: | nix run .#darwin-uninstaller --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} @@ -195,12 +216,19 @@ jobs: switch --flake ~/.config/nix-darwin#simple \ --override-input nix-darwin . \ --override-input nixpkgs nixpkgs/nixpkgs-unstable - - name: Rebuild and activate simple flake, but this time using nix-darwin's flake interface + - name: Switch to new configuration run: | . /etc/static/bashrc + + /usr/bin/sed -i.bak \ + "s/pkgs.vim/pkgs.hello/" \ + ~/.config/nix-darwin/flake.nix + darwin-rebuild switch --flake ~/.config/nix-darwin#simple \ --override-input nix-darwin . \ --override-input nixpkgs nixpkgs/nixpkgs-unstable + + hello - name: Test uninstallation of nix-darwin run: | nix run .#darwin-uninstaller --override-input nixpkgs nixpkgs/nixpkgs-unstable From 2ca27ba780bb072e9fb80565684ad81c2664f9d0 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Tue, 19 Nov 2024 12:50:32 +1100 Subject: [PATCH 10/12] ci: source `/etc/bashrc` instead of `/etc/static/bashrc` This should no longer be necessary after #687, as we can be sure that if activation succeeded, that `/etc/bashrc` points to `/etc/static/bashrc`. --- .github/workflows/test.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d26e0519..6e06117b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,7 +63,7 @@ jobs: -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - name: Switch to new configuration run: | - . /etc/static/bashrc + . /etc/bashrc /usr/bin/sed -i.bak \ "s/pkgs.vim/pkgs.hello/" \ @@ -119,7 +119,7 @@ jobs: -I darwin-config=$HOME/.config/nix-darwin/configuration.nix - name: Switch to new configuration run: | - . /etc/static/bashrc + . /etc/bashrc /usr/bin/sed -i.bak \ "s/pkgs.vim/pkgs.hello/" \ @@ -173,7 +173,7 @@ jobs: --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} - name: Switch to new configuration run: | - . /etc/static/bashrc + . /etc/bashrc /usr/bin/sed -i.bak \ "s/pkgs.vim/pkgs.hello/" \ @@ -218,7 +218,7 @@ jobs: --override-input nixpkgs nixpkgs/nixpkgs-unstable - name: Switch to new configuration run: | - . /etc/static/bashrc + . /etc/bashrc /usr/bin/sed -i.bak \ "s/pkgs.vim/pkgs.hello/" \ From d57e74864bccd31e081443733bfaee1eda85a242 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Tue, 19 Nov 2024 12:54:51 +1100 Subject: [PATCH 11/12] uninstaller: always specify `--extra-experimental-features` first See https://github.com/NixOS/nix/issues/11891 --- .github/workflows/test.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6e06117b..c531aa5f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -74,12 +74,14 @@ jobs: hello - name: Test uninstallation of nix-darwin run: | + # We need to specify `--extra-experimental-features` because `experimental-features` is set by + # `cachix/install-nix-action` but not by our default config above nix run .#darwin-uninstaller \ - --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} \ - --extra-experimental-features "nix-command flakes" + --extra-experimental-features "nix-command flakes" \ + --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} nix run .#darwin-uninstaller.tests.uninstaller \ - --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} \ - --extra-experimental-features "nix-command flakes" + --extra-experimental-features "nix-command flakes" \ + --override-input nixpkgs nixpkgs/${{ env.CURRENT_STABLE_CHANNEL }} - name: Debugging tmate session if: ${{ failure() }} uses: mxschmitt/action-tmate@v3 @@ -130,7 +132,8 @@ jobs: hello - name: Test uninstallation of nix-darwin run: | - # A regression in Nix 2.19 means we need to put `--extra-experimental-features` before `--override-input` + # We need to specify `--extra-experimental-features` because `experimental-features` is set by + # `cachix/install-nix-action` but not by our default config above nix run .#darwin-uninstaller \ --extra-experimental-features "nix-command flakes" \ --override-input nixpkgs nixpkgs/nixpkgs-unstable From 095ba5502c83c5fd8173a1b0dbc99a0e1be7e42d Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Thu, 21 Nov 2024 10:52:11 +1100 Subject: [PATCH 12/12] default: expose all the `darwin-*` commands Change `uninstaller` to `darwin-uninstaller` to match flake attribute --- README.md | 3 ++- default.nix | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2623b9fa..d5373273 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,8 @@ nix-channel --update To install `nix-darwin`, you can just run `darwin-rebuild switch` to install nix-darwin. As `darwin-rebuild` won't be installed in your `PATH` yet, you can use the following command: ```bash -nix --extra-experimental-features "nix-command flakes" run nix-darwin -- switch -I darwin-config=$HOME/.config/nix-darwin/configuration.nix +nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A darwin-rebuild +./result/bin/darwin-rebuild switch -I darwin-config=$HOME/.config/nix-darwin/configuration.nix ``` ### Step 4. Using `nix-darwin` diff --git a/default.nix b/default.nix index 075ae6d2..8bae7e1e 100644 --- a/default.nix +++ b/default.nix @@ -17,5 +17,7 @@ let }; in eval // { - uninstaller = pkgs.callPackage ./pkgs/darwin-uninstaller { }; + darwin-uninstaller = pkgs.callPackage ./pkgs/darwin-uninstaller { }; + + inherit (pkgs.callPackage ./pkgs/nix-tools { }) darwin-option darwin-rebuild darwin-version; }