From 9842f7ea3150089d883f77c82258ae1213b4f700 Mon Sep 17 00:00:00 2001 From: William Hsieh Date: Thu, 5 Sep 2024 15:13:19 +0800 Subject: [PATCH] feat(nix): mkSystem --- config.nix | 22 +++++++++++++ flake.lock | 42 ++++++++++++------------- flake.nix | 82 +++++++++++++++++++------------------------------ lib/default.nix | 54 ++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 72 deletions(-) create mode 100644 config.nix diff --git a/config.nix b/config.nix new file mode 100644 index 00000000..e2917d3b --- /dev/null +++ b/config.nix @@ -0,0 +1,22 @@ +{ + home = { + username = "william"; + fullname = "William Hsieh"; + email = "wh31110@gmail.com"; + dotDir = "dotfiles"; # path relative to $HOME, this example represents `~/dotfiles` + wsl = false; + gui = false; + }; + + nixos = { + # enable = true; + hostname = "nixos-local"; + system = "x86_64-linux"; + }; + + darwin = { + # enable = true; + hostname = "macos-local"; + system = "aarch64-darwin"; + }; +} diff --git a/flake.lock b/flake.lock index accc270f..aa3e5ea6 100644 --- a/flake.lock +++ b/flake.lock @@ -15,6 +15,26 @@ "type": "github" } }, + "darwin": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1724994893, + "narHash": "sha256-yutISDGg6HUaZqCaa54EcsfTwew3vhNtt/FNXBBo44g=", + "owner": "LnL7", + "repo": "nix-darwin", + "rev": "c8d3157d1f768e382de5526bb38e74d2245cad04", + "type": "github" + }, + "original": { + "owner": "LnL7", + "repo": "nix-darwin", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -96,26 +116,6 @@ "type": "github" } }, - "nix-darwin": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1724561770, - "narHash": "sha256-zv8C9RNa86CIpyHwPIVO/k+5TfM8ZbjGwOOpTe1grls=", - "owner": "LnL7", - "repo": "nix-darwin", - "rev": "ac5694a0b855a981e81b4d9f14052e3ff46ca39e", - "type": "github" - }, - "original": { - "owner": "LnL7", - "repo": "nix-darwin", - "type": "github" - } - }, "nix-index-database": { "inputs": { "nixpkgs": [ @@ -187,9 +187,9 @@ "root": { "inputs": { "catppuccin": "catppuccin", + "darwin": "darwin", "git-hooks": "git-hooks", "home-manager": "home-manager", - "nix-darwin": "nix-darwin", "nix-index-database": "nix-index-database", "nixpkgs": "nixpkgs", "nixpkgs-unstable": "nixpkgs-unstable" diff --git a/flake.nix b/flake.nix index c09c3807..3e93af20 100644 --- a/flake.nix +++ b/flake.nix @@ -25,7 +25,7 @@ inputs.nixpkgs.follows = "nixpkgs"; }; - nix-darwin = { + darwin = { url = "github:LnL7/nix-darwin"; inputs.nixpkgs.follows = "nixpkgs"; }; @@ -42,10 +42,12 @@ }; outputs = inputs @ { self, nixpkgs, home-manager, ... }: + with self.lib; let system = "x86_64-linux"; + foreachSystem = genAttrs [ "x86_64-linux" "aarch64-darwin" ]; + username = (import ./home/config.nix).user; - hostname = (import ./system/config.nix).host; # https://github.com/nix-community/home-manager/issues/2942#issuecomment-1378627909 pkgs = import nixpkgs { @@ -64,7 +66,9 @@ inherit (self) outputs; in { - packages.${system}.default = home-manager.defaultPackage.${system}; + lib = import ./lib { inherit inputs; } // inputs.nixpkgs.lib; + + # packages.${system}.default = home-manager.defaultPackage.${system}; homeConfigurations = { ${username} = home-manager.lib.homeManagerConfiguration { @@ -77,36 +81,12 @@ }; }; - nixosConfigurations = { - ${hostname} = nixpkgs.lib.nixosSystem { - specialArgs = { inherit inputs outputs pkgs; }; - modules = [ - ./system/nixos - home-manager.nixosModules.home-manager - { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { inherit inputs outputs; }; - home-manager.users.${username} = import ./home; - } - ]; - }; + nixosConfigurations = lib.mkSystem { + type = "nixos"; }; - darwinConfigurations = { - ${hostname} = inputs.nix-darwin.lib.darwinSystem { - specialArgs = { inherit inputs outputs pkgs; }; - modules = [ - ./system/darwin - home-manager.darwinModules.home-manager - { - home-manager.useGlobalPkgs = true; - # home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { inherit inputs outputs; }; - home-manager.users.${username} = import ./home; - } - ]; - }; + darwinConfigurations = mkSystem { + type = "darwin"; }; # Convenience output that aggregates the outputs for home, nixos, and darwin configurations. @@ -124,25 +104,25 @@ (builtins.attrNames inputs.self.homeConfigurations) (attr: inputs.self.homeConfigurations.${attr}.activationPackage); in - nixtop // hometop; - - checks.${system}.pre-commit-check = inputs.git-hooks.lib.${system}.run { - src = pkgs.lib.cleanSource ./.; - - hooks = { - # TODO: treefmt, selene, shellcheck - editorconfig-checker.enable = true; - nixpkgs-fmt.enable = true; - stylua = { - enable = true; - entry = "${pkgs.stylua}/bin/stylua --config-path ./config/nvim/stylua.toml"; - }; - }; - }; - - devShells.${system}.default = pkgs.mkShell { - inherit (self.checks.${system}.pre-commit-check) shellHook; - buildInputs = self.checks.${system}.pre-commit-check.enabledPackages; - }; + nixtop // darwintop // hometop; + + # checks.${system}.pre-commit-check = inputs.git-hooks.lib.${system}.run { + # src = pkgs.lib.cleanSource ./.; + # + # hooks = { + # # TODO: treefmt, selene, shellcheck + # editorconfig-checker.enable = true; + # nixpkgs-fmt.enable = true; + # stylua = { + # enable = true; + # entry = "${pkgs.stylua}/bin/stylua --config-path ./config/nvim/stylua.toml"; + # }; + # }; + # }; + # + # devShells.${system}.default = pkgs.mkShell { + # inherit (self.checks.${system}.pre-commit-check) shellHook; + # buildInputs = self.checks.${system}.pre-commit-check.enabledPackages; + # }; }; } diff --git a/lib/default.nix b/lib/default.nix index 1406aebe..7661923b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,8 +1,62 @@ { inputs, ... }: +with inputs.nixpkgs.lib; let lockfile = builtins.fromJSON (builtins.readFile ../flake.lock); inherit (inputs.nixpkgs) lib; + + dotfiles = import ../config.nix; + + foreachSystem = genAttrs [ "x86_64-linux" "aarch64-darwin" ]; + + pkgsBySystem = foreachSystem ( + system: + # https://github.com/nix-community/home-manager/issues/2942#issuecomment-1378627909 + import inputs.nixpkgs { + inherit system; + config = { + allowUnfree = true; + allowUnfreePredicate = (_: true); + packageOverrides = pkgs: { + unstable = import inputs.nixpkgs-unstable { + inherit (pkgs) system config; + }; + }; + }; + } + ); + in { stateVersion = "${builtins.elemAt (lib.splitString "-" lockfile.nodes.home-manager.original.ref) 1}"; + + mkSystem = { type }: + let + # TODO: assert type is either "darwin" or "nixos" + isDarwin = type == "darwin"; + osConfig = ../system/${if isDarwin then "darwin" else "nixos" }; + userHMConfig = ../home; + + # NixOS vs nix-darwin functionst + systemFunc = if isDarwin then inputs.darwin.lib.darwinSystem else inputs.nixpkgs.lib.nixosSystem; + osModules = if isDarwin then inputs.home-manager.darwinModules else inputs.home-manager.nixosModules; + + hostSystem = if isDarwin then dotfiles.darwin else dotfiles.nixos; + pkgs = pkgsBySystem.${hostSystem.system}; + in + { + ${hostSystem.hostname} = systemFunc + { + specialArgs = { inherit inputs pkgs dotfiles; }; + modules = [ + osConfig + osModules.home-manager + { + home-manager.useGlobalPkgs = true; + # home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = { inherit inputs dotfiles; }; + home-manager.users.${dotfiles.home.username} = import userHMConfig; + } + ]; + }; + }; }