Dotfiles managed with Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: support host-based darwin and home-manager configs

+235 -29
+35 -4
README.md
··· 1 1 # Dotfiles 2 2 3 - - `flake.nix`: flake inputs and the exported `macos` configuration. 4 - - `macos.nix`: main macOS configuration, plus Homebrew settings. 3 + - `flake.nix`: flake inputs plus exported `darwinConfigurations` and Linux `homeConfigurations`. 4 + - `hosts.nix`: machine inventory keyed by the real hostname of each machine. 5 + - `auto-upgrade.nix`: scheduled `darwin-rebuild` runs plus Nix garbage collection. 6 + - `macos.nix`: macOS system settings managed by `nix-darwin`. 7 + - `home.nix`: shared Home Manager configuration plus small platform-specific conditionals. 5 8 - `packages/shared.nix`: packages you want available everywhere. 6 - - `packages/macos.nix`: packages that only belong to this macOS setup. 7 - - `justfile`: shortcuts for `darwin-rebuild`. 9 + - `packages/macos.nix`: packages that only belong to the macOS setup. 10 + - `justfile`: shortcuts for `darwin-rebuild` and `home-manager`. 11 + 12 + `nix-darwin` stays for macOS because this repo manages system-level things there, such as Homebrew and launchd automation. Linux stays on standalone Home Manager. 13 + 14 + `hosts.nix` is the machine inventory for the whole flake. Each attribute name must match the real short hostname of that machine: 15 + 16 + - macOS: `scutil --get LocalHostName` 17 + - Linux: `hostname -s` 18 + 19 + Each host entry usually only needs: 20 + 21 + - `system`: Nix platform string such as `aarch64-darwin`, `x86_64-linux`, or `aarch64-linux`. 22 + - `username`: local user that Home Manager should manage. 23 + 24 + Optional overrides per host: 25 + 26 + - `homeDirectory`: defaults to `/Users/<username>` on macOS and `/home/<username>` on Linux. 27 + - `flakeDirectory`: defaults to `<homeDirectory>/.dot`. 28 + 29 + To add a new machine, copy an existing entry in `hosts.nix`, rename the key to the real hostname, set `system` and `username`, then add overrides only if that machine uses a non-standard home path or checkout location. 30 + 31 + Useful commands: 32 + 33 + - `just build`: detect the current machine and build its config. 34 + - `just switch`: detect the current machine and switch its config. 35 + - `just hm-build <target>`: build a specific Linux Home Manager target by hostname. 36 + - `just hm-switch <target>`: switch a specific Linux Home Manager target by hostname. 37 + 38 + Automatic upgrade logs live at `/var/log/darwin-auto-upgrade.log`.
+53
auto-upgrade.nix
··· 1 + { config, lib, pkgs, flakeDirectory, hostName, ... }: 2 + 3 + let 4 + flakeDir = flakeDirectory; 5 + flakeRef = "path:${flakeDir}#${hostName}"; 6 + upgradeLogFile = "/var/log/darwin-auto-upgrade.log"; 7 + upgradeScript = pkgs.writeShellScript "darwin-auto-upgrade" '' 8 + set -euo pipefail 9 + 10 + flake_dir=${lib.escapeShellArg flakeDir} 11 + flake_ref=${lib.escapeShellArg flakeRef} 12 + 13 + export HOME="/var/root" 14 + export GIT_CONFIG_COUNT=1 15 + export GIT_CONFIG_KEY_0="safe.directory" 16 + export GIT_CONFIG_VALUE_0="$flake_dir" 17 + 18 + ${config.system.build.darwin-rebuild}/bin/darwin-rebuild switch \ 19 + --flake "$flake_ref" \ 20 + --print-build-logs 21 + ''; 22 + in 23 + { 24 + launchd.daemons.darwin-auto-upgrade = { 25 + command = "${upgradeScript}"; 26 + serviceConfig = { 27 + Label = "dev.vieitesss.darwin-auto-upgrade.${hostName}"; 28 + LowPriorityIO = true; 29 + ProcessType = "Background"; 30 + RunAtLoad = false; 31 + StartCalendarInterval = [ 32 + { 33 + Hour = 2; 34 + Minute = 0; 35 + } 36 + ]; 37 + StandardOutPath = upgradeLogFile; 38 + StandardErrorPath = upgradeLogFile; 39 + }; 40 + }; 41 + 42 + nix.gc = { 43 + automatic = true; 44 + interval = [ 45 + { 46 + Weekday = 7; 47 + Hour = 3; 48 + Minute = 15; 49 + } 50 + ]; 51 + options = "--delete-older-than 30d"; 52 + }; 53 + }
+21
flake.lock
··· 21 21 "type": "github" 22 22 } 23 23 }, 24 + "home-manager": { 25 + "inputs": { 26 + "nixpkgs": [ 27 + "nixpkgs" 28 + ] 29 + }, 30 + "locked": { 31 + "lastModified": 1774539458, 32 + "narHash": "sha256-H9GIOqdMfQZ6lFetsQhO9TCq53hYzpTHDWOt3PRh9V0=", 33 + "owner": "nix-community", 34 + "repo": "home-manager", 35 + "rev": "e2e5f512b33ed19a7a3271d0b73ed5eefcc0be5f", 36 + "type": "github" 37 + }, 38 + "original": { 39 + "owner": "nix-community", 40 + "repo": "home-manager", 41 + "type": "github" 42 + } 43 + }, 24 44 "neovim-nightly-overlay": { 25 45 "inputs": { 26 46 "flake-parts": "flake-parts", ··· 112 132 }, 113 133 "root": { 114 134 "inputs": { 135 + "home-manager": "home-manager", 115 136 "neovim-nightly-overlay": "neovim-nightly-overlay", 116 137 "nix-darwin": "nix-darwin", 117 138 "nixpkgs": "nixpkgs_2"
+61 -8
flake.nix
··· 1 1 { 2 - description = "Personal nix-darwin system flake"; 2 + description = "Personal cross-platform dotfiles flake"; 3 3 4 4 inputs = { 5 5 nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; 6 6 nix-darwin.url = "github:nix-darwin/nix-darwin/master"; 7 7 nix-darwin.inputs.nixpkgs.follows = "nixpkgs"; 8 + home-manager.url = "github:nix-community/home-manager"; 9 + home-manager.inputs.nixpkgs.follows = "nixpkgs"; 8 10 9 11 neovim-nightly-overlay.url = "github:nix-community/neovim-nightly-overlay"; 10 12 }; 11 13 12 - outputs = inputs@{ self, nix-darwin, ... }: { 13 - darwinConfigurations.macos = nix-darwin.lib.darwinSystem { 14 - specialArgs = { inherit inputs self; }; 15 - modules = [ 16 - ./macos.nix 17 - ]; 14 + outputs = inputs@{ self, nixpkgs, nix-darwin, home-manager, ... }: 15 + let 16 + lib = nixpkgs.lib; 17 + isDarwin = host: lib.hasSuffix "darwin" host.system; 18 + mkHost = host: 19 + let 20 + homeDirectory = host.homeDirectory or ( 21 + if isDarwin host then 22 + "/Users/${host.username}" 23 + else 24 + "/home/${host.username}" 25 + ); 26 + in 27 + host // { 28 + inherit homeDirectory; 29 + flakeDirectory = host.flakeDirectory or "${homeDirectory}/.dot"; 30 + }; 31 + hosts = lib.mapAttrs (_: host: mkHost host) (import ./hosts.nix); 32 + mkPkgs = system: import nixpkgs { 33 + inherit system; 34 + config.allowUnfree = true; 35 + }; 36 + mkHomeModule = host: { 37 + imports = [ ./home.nix ]; 38 + home = { 39 + inherit (host) username homeDirectory; 40 + }; 41 + }; 42 + mkHome = _: host: home-manager.lib.homeManagerConfiguration { 43 + pkgs = mkPkgs host.system; 44 + extraSpecialArgs = { 45 + inherit inputs; 46 + }; 47 + modules = [ (mkHomeModule host) ]; 48 + }; 49 + mkDarwin = hostName: host: nix-darwin.lib.darwinSystem { 50 + system = host.system; 51 + specialArgs = { 52 + inherit self hostName; 53 + inherit (host) flakeDirectory username homeDirectory; 54 + }; 55 + modules = [ 56 + ./macos.nix 57 + home-manager.darwinModules.home-manager 58 + { 59 + home-manager.useGlobalPkgs = true; 60 + home-manager.useUserPackages = true; 61 + home-manager.extraSpecialArgs = { 62 + inherit inputs; 63 + }; 64 + home-manager.users.${host.username} = mkHomeModule host; 65 + } 66 + ]; 67 + }; 68 + in { 69 + darwinConfigurations = lib.mapAttrs mkDarwin (lib.filterAttrs (_: host: isDarwin host) hosts); 70 + 71 + homeConfigurations = lib.mapAttrs mkHome (lib.filterAttrs (_: host: !isDarwin host) hosts); 18 72 }; 19 - }; 20 73 }
+26
home.nix
··· 1 + { lib, pkgs, inputs, ... }: 2 + 3 + { 4 + home.packages = 5 + import ./packages/shared.nix { 6 + inherit pkgs inputs; 7 + } 8 + ++ lib.optionals pkgs.stdenv.isDarwin (import ./packages/macos.nix { 9 + inherit pkgs; 10 + }); 11 + 12 + targets.genericLinux.enable = pkgs.stdenv.isLinux; 13 + 14 + # This value determines the Home Manager release that your 15 + # configuration is compatible with. This helps avoid breakage 16 + # when a new Home Manager release introduces backwards 17 + # incompatible changes. 18 + # 19 + # You can update Home Manager without changing this value. See 20 + # the Home Manager release notes for a list of state version 21 + # changes in each release. 22 + home.stateVersion = "25.11"; 23 + 24 + # Let Home Manager install and manage itself. 25 + programs.home-manager.enable = true; 26 + }
+19
hosts.nix
··· 1 + { 2 + # Host entries are keyed by the machine's real short hostname. 3 + # - macOS: `scutil --get LocalHostName` 4 + # - Linux: `hostname -s` 5 + # 6 + # Each host usually only needs: 7 + # - `system`: Nix platform string such as `aarch64-darwin` or `x86_64-linux` 8 + # Get it on the target machine with: 9 + # `nix eval --impure --raw --expr builtins.currentSystem` 10 + # - `username`: local login user that Home Manager should manage 11 + # 12 + # Optional overrides: 13 + # - `homeDirectory`: defaults to `/Users/<username>` on macOS and `/home/<username>` on Linux 14 + # - `flakeDirectory`: defaults to `<homeDirectory>/.dot` 15 + MacBook-Air-de-Daniel = { 16 + system = "aarch64-darwin"; 17 + username = "vieitesprefapp"; 18 + }; 19 + }
+11 -3
justfile
··· 1 - host := "macos" 1 + current_host := `if [ "$(uname -s)" = "Darwin" ]; then scutil --get LocalHostName; else hostname -s; fi` 2 + current_flake := "path:.#{{current_host}}" 2 3 3 4 alias drs := switch 5 + alias hms := hm-switch 4 6 5 7 _default: 6 8 just -l 7 9 8 10 build: 9 - darwin-rebuild build --flake .#{{host}} 11 + printf 'Using host %s\n' "{{current_host}}"; if [ "$(uname -s)" = "Darwin" ]; then darwin-rebuild build --flake "{{current_flake}}"; else home-manager build --flake "{{current_flake}}"; fi 10 12 11 13 switch: 12 - sudo darwin-rebuild switch --flake .#{{host}} 14 + printf 'Using host %s\n' "{{current_host}}"; if [ "$(uname -s)" = "Darwin" ]; then sudo darwin-rebuild switch --flake "{{current_flake}}"; else home-manager switch --flake "{{current_flake}}"; fi 15 + 16 + hm-build target: 17 + home-manager build --flake path:.#{{target}} 18 + 19 + hm-switch target: 20 + home-manager switch --flake path:.#{{target}}
+9 -14
macos.nix
··· 1 - { pkgs, inputs, self, ... }: 2 - let 3 - sharedPackages = import ./packages/shared.nix { 4 - inherit pkgs inputs; 5 - }; 1 + { self, username, homeDirectory, ... }: 2 + { 3 + imports = [ ./auto-upgrade.nix ]; 6 4 7 - macosPackages = import ./packages/macos.nix { 8 - inherit pkgs; 9 - }; 10 - in 11 - { 12 5 nix.settings.experimental-features = "nix-command flakes"; 13 6 14 - nixpkgs.hostPlatform = "aarch64-darwin"; 15 7 nixpkgs.config.allowUnfree = true; 16 8 17 - system.primaryUser = "vieitesprefapp"; 9 + users.users.${username} = { 10 + name = username; 11 + home = homeDirectory; 12 + }; 13 + 14 + system.primaryUser = username; 18 15 system.stateVersion = 6; 19 16 system.configurationRevision = self.rev or self.dirtyRev or null; 20 - 21 - environment.systemPackages = sharedPackages ++ macosPackages; 22 17 23 18 homebrew = { 24 19 enable = true;