From a1b282277e01bf810060eff5a0a4ca4ca9015ce0 Mon Sep 17 00:00:00 2001 From: winston Date: Wed, 14 Jun 2023 03:09:32 +0200 Subject: [PATCH] feat(flake-parts)!: refactor to flake-parts --- flake.lock | 102 +++++++++++++++++++---------- flake.nix | 170 +++++++++++++++++------------------------------- lib/default.nix | 77 ++++++++++++++++++++++ 3 files changed, 205 insertions(+), 144 deletions(-) create mode 100644 lib/default.nix diff --git a/flake.lock b/flake.lock index 5eb62b7..724fc5c 100644 --- a/flake.lock +++ b/flake.lock @@ -7,17 +7,16 @@ ] }, "locked": { - "lastModified": 1685836117, - "narHash": "sha256-Z1MECnYm0qznV7khdtDR2wnQgVsX/+gc716ztjtSRWU=", - "owner": "nekowinston", - "repo": "caarlos0-nur", - "rev": "7494c6ec1eb21b722ef0f131fa2afa0e80e0b81a", + "lastModified": 1686575758, + "narHash": "sha256-m2eeo5ClbunNt9DUXCyNyNDuH0rOZ1Xb/Njqb+zp8G0=", + "owner": "caarlos0", + "repo": "nur", + "rev": "c87e77a4bc47b291d4f5c551dbbef52f27e13425", "type": "github" }, "original": { - "owner": "nekowinston", - "ref": "feat/add-apple-music-discord-module", - "repo": "caarlos0-nur", + "owner": "caarlos0", + "repo": "nur", "type": "github" } }, @@ -114,6 +113,24 @@ "type": "github" } }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1685662779, + "narHash": "sha256-cKDDciXGpMEjP1n6HlzKinN0H+oLmNpgeCTzYnsA2po=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "71fb97f0d875fd4de4994dfb849f2c75e17eb6c3", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" @@ -178,11 +195,11 @@ ] }, "locked": { - "lastModified": 1686604884, - "narHash": "sha256-AkfxSmGGvNMtyXt1us9Lm8cMeIwqxpkSTeNeBQ00SL8=", + "lastModified": 1686693747, + "narHash": "sha256-pf0rcQeKejsBfv6NPZCXAo1gN2XtRYm2jzRwHswYeEc=", "owner": "nix-community", "repo": "home-manager", - "rev": "b01eb1eb3b579c74e6a4189ef33cc3fa24c40613", + "rev": "62f111ef1e50e518dbb3f0782a31dcd244a0a2d7", "type": "github" }, "original": { @@ -198,11 +215,11 @@ "wezterm-src": "wezterm-src" }, "locked": { - "lastModified": 1686553559, - "narHash": "sha256-3GpWAkqKzo2L1WO4e9iWom8CxpompgH6le5tP1KG80o=", + "lastModified": 1686643608, + "narHash": "sha256-rnUWWZF5gA+sI1pNZ+xJfyzBYsJ2mbgln4FAtgxmir4=", "owner": "nekowinston", "repo": "nur", - "rev": "69fef37c252ac8c21aaf4192e1b7d1b87a525054", + "rev": "da20835c7211634852f2617c93d167eae45d9aeb", "type": "github" }, "original": { @@ -218,11 +235,11 @@ ] }, "locked": { - "lastModified": 1686574167, - "narHash": "sha256-hxE8z+S9E4Qw03D2VQRaJUmj9zep3FvhKz316JUZuPA=", + "lastModified": 1686671165, + "narHash": "sha256-q3Poq8FlulxDMCETXL0ehb9J3h5Naf5yGh1x42RXXRI=", "owner": "Mic92", "repo": "nix-index-database", - "rev": "7e83b70f31f4483c07e6939166cb667ecb8d05d5", + "rev": "14eede9450dc45b92adc9f1dfac873eac7e1a5de", "type": "github" }, "original": { @@ -233,11 +250,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1686488075, - "narHash": "sha256-2otSBt2hbeD+5yY25NF3RhWx7l5SDt1aeU3cJ/9My4M=", + "lastModified": 1686519857, + "narHash": "sha256-VkBhuq67aXXiCoEmicziuDLUPPjeOTLQoj6OeVai5zM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "9401a0c780b49faf6c28adf55764f230301d0dce", + "rev": "6b1b72c0f887a478a5aac355674ff6df0fc44f44", "type": "github" }, "original": { @@ -247,13 +264,31 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1685564631, + "narHash": "sha256-8ywr3AkblY4++3lIVxmrWZFzac7+f32ZEhH/A8pNscI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4f53efe34b3a8877ac923b9350c874e3dcd5dc0a", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "nixpkgs_2": { "locked": { - "lastModified": 1686519857, - "narHash": "sha256-VkBhuq67aXXiCoEmicziuDLUPPjeOTLQoj6OeVai5zM=", + "lastModified": 1686582075, + "narHash": "sha256-vtflsfKkHtF8IduxDNtbme4cojiqvlvjp5QNYhvoHXc=", "owner": "nixos", "repo": "nixpkgs", - "rev": "6b1b72c0f887a478a5aac355674ff6df0fc44f44", + "rev": "7e63eed145566cca98158613f3700515b4009ce3", "type": "github" }, "original": { @@ -265,11 +300,11 @@ }, "nur": { "locked": { - "lastModified": 1686634533, - "narHash": "sha256-YszZkcJfhQL3m2zBNT8hLpA+DNCcVxSHK2/3jNa9EMY=", + "lastModified": 1686696247, + "narHash": "sha256-gZL5rk1iySrvKbdw8NU5BaSDWuWA5O7ZWw+j+vxXjc4=", "owner": "nix-community", "repo": "nur", - "rev": "875deba73f2360a885d1b81c3d88218bce4c1230", + "rev": "c3677b051af4921de6e184749035d08859e4ed62", "type": "github" }, "original": { @@ -293,11 +328,11 @@ ] }, "locked": { - "lastModified": 1686213770, - "narHash": "sha256-Re6xXLEqQ/HRnThryumyGzEf3Uv0Pl4cuG50MrDofP8=", + "lastModified": 1686668298, + "narHash": "sha256-AADh9NqHh6X2LOem4BvI7oCkMm+JPCSCE7iIw5nn0VA=", "owner": "cachix", "repo": "pre-commit-hooks.nix", - "rev": "182af51202998af5b64ddecaa7ff9be06425399b", + "rev": "5b6b54d3f722aa95cbf4ddbe35390a0af8c0015a", "type": "github" }, "original": { @@ -310,6 +345,7 @@ "inputs": { "caarlos0-nur": "caarlos0-nur", "darwin": "darwin", + "flake-parts": "flake-parts", "flake-utils": "flake-utils", "home-manager": "home-manager", "nekowinston-nur": "nekowinston-nur", @@ -425,11 +461,11 @@ "wezterm-src": { "flake": false, "locked": { - "lastModified": 1685453249, - "narHash": "sha256-w/iQoWng7nrE+HAg0umpq8RCU0bjw5JogR2HHufspI8=", + "lastModified": 1686577193, + "narHash": "sha256-9dCCT3Nd6XAZBKIlbqKQZj8ruUudtwcbd3CYJIK9Dl8=", "ref": "refs/heads/main", - "rev": "95e44f2199d9779e353bccf387a1eb2dbaf41f44", - "revCount": 7255, + "rev": "baf9d970816e015bee41ed5eb9186ef7f71c454c", + "revCount": 7256, "submodules": true, "type": "git", "url": "https://github.com/wez/wezterm" diff --git a/flake.nix b/flake.nix index 1d93406..3742d4d 100644 --- a/flake.nix +++ b/flake.nix @@ -8,10 +8,9 @@ home-manager.url = "github:nix-community/home-manager"; home-manager.inputs.nixpkgs.follows = "nixpkgs"; - # NUR nur.url = "github:nix-community/nur"; nekowinston-nur.url = "github:nekowinston/nur"; - caarlos0-nur.url = "github:nekowinston/caarlos0-nur/feat/add-apple-music-discord-module"; + caarlos0-nur.url = "github:caarlos0/nur"; caarlos0-nur.inputs.nixpkgs.follows = "nixpkgs"; sops.url = "github:Mic92/sops-nix"; @@ -28,8 +27,66 @@ pre-commit-hooks.inputs.flake-utils.follows = "flake-utils"; pre-commit-hooks.inputs.nixpkgs-stable.follows = "nixpkgs"; flake-utils.url = "github:numtide/flake-utils"; + flake-parts.url = "github:hercules-ci/flake-parts"; }; + outputs = {flake-parts, ...} @ inputs: let + inherit (import ./lib {inherit inputs;}) mkSystems overlays; + in + flake-parts.lib.mkFlake {inherit inputs;} + { + systems = ["aarch64-darwin" "aarch64-linux" "x86_64-darwin" "x86_64-linux"]; + flake = mkSystems [ + { + host = "sashimi"; + system = "aarch64-darwin"; + username = "winston"; + } + { + host = "futomaki"; + system = "x86_64-linux"; + username = "winston"; + } + { + host = "bento"; + system = "x86_64-linux"; + username = "w"; + } + ]; + perSystem = { + config, + self', + inputs', + pkgs, + system, + ... + }: { + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + overlays = [overlays]; + }; + + checks.pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + alejandra.enable = true; + commitizen.enable = true; + editorconfig-checker.enable = true; + luacheck.enable = true; + nil.enable = true; + shellcheck.enable = true; + stylua.enable = true; + }; + }; + + devShells.default = pkgs.mkShell { + name = "nixpkgs"; + inherit (self'.checks.pre-commit-check) shellHook; + nativeBuildInputs = with pkgs; [git-crypt just sops]; + }; + }; + }; + nixConfig = { extra-substituters = [ "https://cache.garnix.io" @@ -49,113 +106,4 @@ tarball-ttl = 604800; warn-dirty = false; }; - - outputs = { - self, - darwin, - home-manager, - nixpkgs, - ... - } @ inputs: let - overlays = final: prev: { - nur = import inputs.nur { - nurpkgs = prev; - pkgs = prev; - repoOverrides = { - caarlos0 = inputs.caarlos0-nur.packages.${prev.system}; - nekowinston = inputs.nekowinston-nur.packages.${prev.system}; - }; - }; - sway-unwrapped = inputs.swayfx.packages.${prev.system}.default; - }; - commonHMConfig = {username}: ({ - config, - pkgs, - ... - }: { - config = { - nixpkgs.overlays = [overlays]; - home-manager = { - useGlobalPkgs = true; - useUserPackages = true; - backupFileExtension = "backup"; - sharedModules = [ - inputs.nix-index-database.hmModules.nix-index - inputs.sops.homeManagerModules.sops - inputs.caarlos0-nur.homeManagerModules.default - inputs.nekowinston-nur.homeManagerModules.default - ]; - users.${username}.imports = [./home]; - extraSpecialArgs = { - flakePath = - if pkgs.stdenv.isDarwin - then "/Users/${username}/.config/nixpkgs" - else "/home/${username}/.config/nixpkgs"; - }; - }; - }; - }); - in - { - nixosConfigurations = { - "futomaki" = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - modules = [ - home-manager.nixosModules.home-manager - ./machines/common - ./machines/futomaki - (commonHMConfig { - username = "winston"; - }) - ]; - }; - "bento" = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - modules = [ - home-manager.nixosModules.home-manager - ./machines/common - ./machines/bento - (commonHMConfig { - username = "w"; - }) - ]; - }; - }; - darwinConfigurations = { - "sashimi" = darwin.lib.darwinSystem { - system = "aarch64-darwin"; - modules = [ - home-manager.darwinModules.home-manager - inputs.nekowinston-nur.darwinModules.default - ./machines/common - ./machines/sashimi - (commonHMConfig { - username = "winston"; - }) - ]; - }; - }; - } - // inputs.flake-utils.lib.eachDefaultSystem (system: let - pkgs = nixpkgs.legacyPackages.${system}; - in { - checks.pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - alejandra.enable = true; - commitizen.enable = true; - editorconfig-checker.enable = true; - luacheck.enable = true; - nil.enable = true; - shellcheck.enable = true; - stylua.enable = true; - }; - }; - devShells.default = pkgs.mkShell { - name = "nixpkgs"; - inherit (self.checks.${system}.pre-commit-check) shellHook; - nativeBuildInputs = [pkgs.just pkgs.git-crypt pkgs.sops]; - }; - formatter = pkgs.alejandra; - }); } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..d51cf6d --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,77 @@ +{inputs}: rec { + overlays = final: prev: { + nur = import inputs.nur { + nurpkgs = prev; + pkgs = prev; + repoOverrides = { + caarlos0 = inputs.caarlos0-nur.packages.${prev.system}; + nekowinston = inputs.nekowinston-nur.packages.${prev.system}; + }; + }; + sway-unwrapped = inputs.swayfx.packages.${prev.system}.default; + }; + commonHMConfig = {username}: ({ + config, + pkgs, + ... + }: { + config = { + nixpkgs.overlays = [overlays]; + home-manager = { + backupFileExtension = "backup"; + useGlobalPkgs = true; + useUserPackages = true; + sharedModules = with inputs; [ + nix-index-database.hmModules.nix-index + sops.homeManagerModules.sops + caarlos0-nur.homeManagerModules.default + nekowinston-nur.homeManagerModules.default + ]; + users.${username}.imports = [../home]; + extraSpecialArgs = { + flakePath = + if pkgs.stdenv.isDarwin + then "/Users/${username}/.config/nixpkgs" + else "/home/${username}/.config/nixpkgs"; + }; + }; + }; + }); + mkMerge = contents: { + _type = "merge"; + inherit contents; + }; + mkSystem = { + host, + system, + username, + extraModules ? [], + }: let + target = + if isLinux + then "nixosConfigurations" + else if isDarwin + then "darwinConfigurations" + else throw "Unsupported system"; + builder = + if isLinux + then inputs.nixpkgs.lib.nixosSystem + else if isDarwin + then inputs.darwin.lib.darwinSystem + else throw "Unsupported system"; + pkgs = inputs.nixpkgs.legacyPackages.${system}; + inherit (pkgs) lib; + inherit (pkgs.stdenv) isLinux isDarwin; + in { + ${target}."${host}" = builder { + inherit system; + modules = + [../machines/common ../machines/${host}] + ++ lib.optionals isLinux [inputs.home-manager.nixosModules.home-manager] + ++ lib.optionals isDarwin [inputs.home-manager.darwinModules.home-manager] + ++ [(commonHMConfig {inherit username;})] + ++ extraModules; + }; + }; + mkSystems = systems: mkMerge (map mkSystem systems); +}