diff --git a/README.md b/README.md index 8ed3e95..70b6a25 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ just fmt hosts/darwin/ - macOS host entrypoint hosts/netty/ - NixOS VPS entrypoint (disko + hardware) modules/ - shared system modules + devshells +modules/hosts/ - flake-parts host output definitions +modules/nixpkgs.nix - shared flake context (hosts, args, pkgs helpers) home/ - Home Manager modules lib/hosts.nix - host metadata used by the flake lib/ - shared package sets and theme system diff --git a/flake.nix b/flake.nix index a95a5f6..5b340ef 100644 --- a/flake.nix +++ b/flake.nix @@ -45,98 +45,13 @@ }; outputs = - inputs@{ - self, - flake-parts, - nixpkgs, - nix-darwin, - home-manager, - nix-homebrew, - ... - }: - let - username = "rathi"; - hosts = import ./lib/hosts.nix { inherit username; }; - - mkPkgs = - system: - import nixpkgs { - inherit system; - config.allowUnfree = true; - }; - - mkHomeManagerModule = - host: - { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { - inherit inputs self username; - hostname = host.hostname; - }; - home-manager.backupFileExtension = "hm-bak"; - home-manager.users.${username} = import host.homeModule; - }; - in + inputs@{ flake-parts, ... }: flake-parts.lib.mkFlake { inherit inputs; } { - systems = [ - hosts.darwin.system - hosts.netty.system - ]; - imports = [ + ./modules/nixpkgs.nix ./modules/devshells.nix + ./modules/hosts/darwin.nix + ./modules/hosts/netty.nix ]; - - flake = { - darwinConfigurations.${hosts.darwin.name} = nix-darwin.lib.darwinSystem { - system = hosts.darwin.system; - specialArgs = { - inherit inputs self username; - hostname = hosts.darwin.hostname; - }; - modules = [ - ./hosts/${hosts.darwin.name} - home-manager.darwinModules.home-manager - nix-homebrew.darwinModules.nix-homebrew - { - users.users.${username}.home = hosts.darwin.homeDirectory; - - nix-homebrew = { - enable = true; - enableRosetta = true; - user = username; - autoMigrate = true; - }; - } - (mkHomeManagerModule hosts.darwin) - ]; - }; - - nixosConfigurations.${hosts.netty.name} = nixpkgs.lib.nixosSystem { - system = hosts.netty.system; - specialArgs = { - inherit inputs self username; - hostname = hosts.netty.hostname; - }; - modules = [ - inputs.disko.nixosModules.disko - ./hosts/${hosts.netty.name}/configuration.nix - home-manager.nixosModules.home-manager - (mkHomeManagerModule hosts.netty) - ]; - }; - - homeConfigurations.${hosts.netty.name} = home-manager.lib.homeManagerConfiguration { - pkgs = mkPkgs hosts.netty.system; - extraSpecialArgs = { - inherit inputs self username; - hostname = hosts.netty.hostname; - }; - modules = [ - hosts.netty.standaloneHomeModule - ]; - }; - }; }; } diff --git a/justfile b/justfile index 8a0c150..ee60ce5 100644 --- a/justfile +++ b/justfile @@ -17,7 +17,8 @@ switch config='darwin': if [[ "{{config}}" == "darwin" ]]; then sudo env PATH="$PATH" nix --extra-experimental-features 'nix-command flakes' run github:nix-darwin/nix-darwin/master#darwin-rebuild -- switch --flake path:.#{{config}} else - nix --extra-experimental-features 'nix-command flakes' run github:nix-community/home-manager -- switch --flake path:.#{{config}} -b hm-bak + backup_ext="hm-bak-$(date +%Y%m%d-%H%M%S)" + nix --extra-experimental-features 'nix-command flakes' run github:nix-community/home-manager -- switch --flake path:.#{{config}} -b "$backup_ext" fi fmt: diff --git a/modules/hosts/darwin.nix b/modules/hosts/darwin.nix new file mode 100644 index 0000000..12320eb --- /dev/null +++ b/modules/hosts/darwin.nix @@ -0,0 +1,33 @@ +{ + hosts, + inputs, + username, + mkSpecialArgs, + mkHomeManagerModule, + ... +}: +let + host = hosts.darwin; +in +{ + flake.darwinConfigurations.${host.name} = inputs.nix-darwin.lib.darwinSystem { + system = host.system; + specialArgs = mkSpecialArgs host; + modules = [ + ../../hosts/${host.name} + inputs.home-manager.darwinModules.home-manager + inputs.nix-homebrew.darwinModules.nix-homebrew + { + users.users.${username}.home = host.homeDirectory; + + nix-homebrew = { + enable = true; + enableRosetta = true; + user = username; + autoMigrate = true; + }; + } + (mkHomeManagerModule host) + ]; + }; +} diff --git a/modules/hosts/netty.nix b/modules/hosts/netty.nix new file mode 100644 index 0000000..a5d090e --- /dev/null +++ b/modules/hosts/netty.nix @@ -0,0 +1,33 @@ +{ + hosts, + inputs, + mkPkgs, + mkSpecialArgs, + mkHomeManagerModule, + ... +}: +let + host = hosts.netty; +in +{ + flake = { + nixosConfigurations.${host.name} = inputs.nixpkgs.lib.nixosSystem { + system = host.system; + specialArgs = mkSpecialArgs host; + modules = [ + inputs.disko.nixosModules.disko + ../../hosts/${host.name}/configuration.nix + inputs.home-manager.nixosModules.home-manager + (mkHomeManagerModule host) + ]; + }; + + homeConfigurations.${host.name} = inputs.home-manager.lib.homeManagerConfiguration { + pkgs = mkPkgs host.system; + extraSpecialArgs = mkSpecialArgs host; + modules = [ + host.standaloneHomeModule + ]; + }; + }; +} diff --git a/modules/nixpkgs.nix b/modules/nixpkgs.nix new file mode 100644 index 0000000..cbc9007 --- /dev/null +++ b/modules/nixpkgs.nix @@ -0,0 +1,45 @@ +{ + self, + inputs, + lib, + ... +}: +let + username = "rathi"; + hosts = import ../lib/hosts.nix { inherit username; }; + + mkPkgs = + system: + import inputs.nixpkgs { + inherit system; + config.allowUnfree = true; + }; + + mkSpecialArgs = host: { + inherit inputs self username; + hostname = host.hostname; + }; + + mkHomeManagerModule = + host: + { + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = mkSpecialArgs host; + home-manager.backupCommand = "bash ${../scripts/home-manager-backup.sh}"; + home-manager.users.${username} = import host.homeModule; + }; +in +{ + systems = lib.unique (map (host: host.system) (builtins.attrValues hosts)); + + _module.args = { + inherit + username + hosts + mkPkgs + mkSpecialArgs + mkHomeManagerModule + ; + }; +} diff --git a/scripts/home-manager-backup.sh b/scripts/home-manager-backup.sh new file mode 100644 index 0000000..f093f91 --- /dev/null +++ b/scripts/home-manager-backup.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [[ $# -eq 0 ]]; then + exit 0 +fi + +target_path="${1:?expected target path}" +base_backup="${target_path}.hm-bak" + +if [[ ! -e "$base_backup" ]]; then + mv "$target_path" "$base_backup" + exit 0 +fi + +timestamp="$(date +%Y%m%d-%H%M%S)" +backup_path="${base_backup}.${timestamp}" +suffix=0 + +while [[ -e "$backup_path" ]]; do + suffix=$((suffix + 1)) + backup_path="${base_backup}.${timestamp}.${suffix}" +done + +mv "$target_path" "$backup_path"