From 80d848cfa3731ff7f4b79e95bd73999fcc8dcb46 Mon Sep 17 00:00:00 2001 From: "Simen A. W. Olsen" Date: Sat, 15 Aug 2026 00:34:17 +0200 Subject: feat(darwin): add makkie host config and parameterize per-machine settings --- INSTALL.md | 26 +++++++++++++++-- README.md | 7 +++-- flake.nix | 69 ++++++++++++++++++++++++++++++++++------------ modules/darwin/default.nix | 3 -- modules/home/zsh.nix | 6 ++-- 5 files changed, 84 insertions(+), 27 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 805b776..024695d 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -62,8 +62,16 @@ installed yet. After the first switch, git rewrites the HTTPS remote to SSH ### 5. First switch (bootstrap nix-darwin) +Pick the configuration that matches the machine — each one pins its own username, +so switching with the wrong attribute fails during activation: + +| Attribute | Machine | User | +| --------- | ------- | ---- | +| `makkie` | MacBook Pro (sets its hostname to `makkie`) | `simenandre` | +| `default` | Older MacBook Pro | `cobraz` | + ```shell -sudo nix run nix-darwin#darwin-rebuild -- switch --flake .#default +sudo nix run nix-darwin#darwin-rebuild -- switch --flake .#makkie ``` This is the long one: it installs every Homebrew cask, the Mac App Store apps, @@ -73,7 +81,21 @@ for your password, and expect to approve a few system dialogs. Subsequent switches use the installed binary: ```shell -sudo darwin-rebuild switch --flake .#default +sudo darwin-rebuild switch --flake .#makkie +``` + +The `rebuild` and `update` aliases already carry the right attribute for the +machine they were built for, so after the first switch you can just run +`rebuild`. + +Adding another Mac later means one entry in `flake.nix`: + +```nix +"newhost" = mkDarwin { + username = "simenandre"; + flakeAttr = "newhost"; + hostName = "newhost"; +}; ``` Restart the terminal (or log out and back in) so the new zsh config, `PATH`, and diff --git a/README.md b/README.md index 12185ad..dbca951 100644 --- a/README.md +++ b/README.md @@ -25,16 +25,19 @@ cd ~/ghq/simenandre.no/dotfiles #### macOS +Configurations are per-machine — `makkie` (user `simenandre`) and `default` +(user `cobraz`). Substitute the one you are on. + On first run, bootstrap nix-darwin: ```shell -sudo nix run nix-darwin#darwin-rebuild -- switch --flake .#default +sudo nix run nix-darwin#darwin-rebuild -- switch --flake .#makkie ``` On subsequent runs: ```shell -sudo darwin-rebuild switch --flake .#default +sudo darwin-rebuild switch --flake .#makkie ``` #### NixOS diff --git a/flake.nix b/flake.nix index db317d9..41ad456 100644 --- a/flake.nix +++ b/flake.nix @@ -35,28 +35,63 @@ dina, }: let - mkHomeModules = username: [ + # flakeAttr must match the darwinConfigurations attribute this machine is + # built from; the rebuild/update aliases in modules/home/zsh.nix use it so + # each machine targets its own configuration. + mkHomeModules = { username, flakeAttr ? "default" }: [ ./modules/home { home.username = username; home.file.".config/nvim".source = config-nvim; + _module.args.flakeAttr = flakeAttr; } ]; + + mkDarwin = + { + username, + flakeAttr, + hostName ? null, + }: + nix-darwin.lib.darwinSystem { + system = "aarch64-darwin"; + modules = [ + ./modules/darwin + home-manager.darwinModules.home-manager + { + # Primary user for system defaults and homebrew + system.primaryUser = username; + + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.backupFileExtension = "bak"; + home-manager.extraSpecialArgs = { inherit dina; }; + home-manager.users.${username} = { + imports = mkHomeModules { inherit username flakeAttr; }; + }; + } + ] + ++ nixpkgs.lib.optional (hostName != null) { + networking = { + inherit hostName; + computerName = hostName; + localHostName = hostName; + }; + }; + }; in { - darwinConfigurations."default" = nix-darwin.lib.darwinSystem { - system = "aarch64-darwin"; - modules = [ - ./modules/darwin - home-manager.darwinModules.home-manager - { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.backupFileExtension = "bak"; - home-manager.extraSpecialArgs = { inherit dina; }; - home-manager.users.cobraz = { imports = mkHomeModules "cobraz"; }; - } - ]; + darwinConfigurations = { + "makkie" = mkDarwin { + username = "simenandre"; + flakeAttr = "makkie"; + hostName = "makkie"; + }; + + "default" = mkDarwin { + username = "cobraz"; + flakeAttr = "default"; + }; }; nixosConfigurations."nixbox" = nixpkgs.lib.nixosSystem { @@ -70,7 +105,7 @@ home-manager.useUserPackages = true; home-manager.backupFileExtension = "bak"; home-manager.extraSpecialArgs = { inherit dina; }; - home-manager.users.simenandre = { imports = mkHomeModules "simenandre"; }; + home-manager.users.simenandre = { imports = mkHomeModules { username = "simenandre"; }; }; } ]; }; @@ -78,13 +113,13 @@ homeConfigurations."simenandre" = home-manager.lib.homeManagerConfiguration { pkgs = nixpkgs.legacyPackages."x86_64-linux"; extraSpecialArgs = { inherit dina; }; - modules = mkHomeModules "simenandre"; + modules = mkHomeModules { username = "simenandre"; }; }; homeConfigurations."simenandre@aarch64-linux" = home-manager.lib.homeManagerConfiguration { pkgs = nixpkgs.legacyPackages."aarch64-linux"; extraSpecialArgs = { inherit dina; }; - modules = mkHomeModules "simenandre"; + modules = mkHomeModules { username = "simenandre"; }; }; }; } diff --git a/modules/darwin/default.nix b/modules/darwin/default.nix index b4b81a8..90f9ab2 100644 --- a/modules/darwin/default.nix +++ b/modules/darwin/default.nix @@ -16,9 +16,6 @@ # System state version – do not change after first installation system.stateVersion = 5; - # Primary user for system defaults and homebrew - system.primaryUser = "cobraz"; - # Enable Touch ID for sudo security.pam.services.sudo_local.touchIdAuth = true; diff --git a/modules/home/zsh.nix b/modules/home/zsh.nix index a7817f9..1c114b2 100644 --- a/modules/home/zsh.nix +++ b/modules/home/zsh.nix @@ -1,4 +1,4 @@ -{ pkgs, lib, ... }: +{ pkgs, lib, flakeAttr, ... }: let # Pinned to commits, not branch names: a branch ref keeps moving while the @@ -117,8 +117,8 @@ in alias ls='ls --color' '' + (if pkgs.stdenv.isDarwin then '' alias tailscale='/Applications/Tailscale.app/Contents/MacOS/Tailscale' - alias rebuild='sudo darwin-rebuild switch --flake ~/ghq/simenandre.no/dotfiles#default' - alias update='nix flake update --flake ~/ghq/simenandre.no/dotfiles && sudo darwin-rebuild switch --flake ~/ghq/simenandre.no/dotfiles#default' + alias rebuild='sudo darwin-rebuild switch --flake ~/ghq/simenandre.no/dotfiles#${flakeAttr}' + alias update='nix flake update --flake ~/ghq/simenandre.no/dotfiles && sudo darwin-rebuild switch --flake ~/ghq/simenandre.no/dotfiles#${flakeAttr}' alias nix-gc='sudo nix-collect-garbage --delete-older-than 14d && nix-collect-garbage --delete-older-than 14d && nix store gc' '' else "") + '' -- cgit v1.2.3