From 5bb485277fec92a5cb47b1132434aa3ada4cf03e Mon Sep 17 00:00:00 2001 From: a-kenji Date: Thu, 10 Oct 2024 00:08:02 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20move=20to=20`flake-parts`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flake.lock | 31 ++++ flake.nix | 162 +----------------- formatter.nix | 12 -- nix/checks.nix | 16 ++ nix/crane.nix | 83 +++++++++ nix/default.nix | 15 ++ nix/devshells.nix | 19 ++ nix/env.nix | 7 + nix/formatter.nix | 19 ++ .../home-manager-module.nix | 0 nix/lib.nix | 10 ++ nix/meta.nix | 9 + nix/packages.nix | 13 ++ 13 files changed, 224 insertions(+), 172 deletions(-) delete mode 100644 formatter.nix create mode 100644 nix/checks.nix create mode 100644 nix/crane.nix create mode 100644 nix/default.nix create mode 100644 nix/devshells.nix create mode 100644 nix/env.nix create mode 100644 nix/formatter.nix rename home-manager-module.nix => nix/home-manager-module.nix (100%) create mode 100644 nix/lib.nix create mode 100644 nix/meta.nix create mode 100644 nix/packages.nix diff --git a/flake.lock b/flake.lock index d98d88d..8a8192a 100644 --- a/flake.lock +++ b/flake.lock @@ -15,6 +15,24 @@ "type": "github" } }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1727826117, + "narHash": "sha256-K5ZLCyfO/Zj9mPFldf3iwS6oZStJcU4tSpiXTMYaaL0=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "3d04084d54bedc3d6b8b736c70ef449225c361b1", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "home-manager": { "inputs": { "nixpkgs": "nixpkgs" @@ -49,6 +67,18 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1727825735, + "narHash": "sha256-0xHYkMkeLVQAMa7gvkddbPqpxph+hDzdu1XdGPJR+Os=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/fb192fec7cc7a4c26d51779e9bab07ce6fa5597a.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/fb192fec7cc7a4c26d51779e9bab07ce6fa5597a.tar.gz" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1727296349, @@ -84,6 +114,7 @@ "root": { "inputs": { "crane": "crane", + "flake-parts": "flake-parts", "home-manager": "home-manager", "nixpkgs": "nixpkgs_2", "treefmt-nix": "treefmt-nix" diff --git a/flake.nix b/flake.nix index 2f418c7..d29698d 100644 --- a/flake.nix +++ b/flake.nix @@ -8,6 +8,7 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; home-manager.url = "github:nix-community/home-manager"; treefmt-nix.url = "github:numtide/treefmt-nix/"; crane = { @@ -16,164 +17,5 @@ }; }; - outputs = - { - self, - nixpkgs, - crane, - treefmt-nix, - home-manager, - ... - }: - let - system = "x86_64-linux"; - pkgs = import nixpkgs { inherit system; }; - - devInputs = with pkgs; [ - rustc - rustfmt - cargo - clippy - ]; - - nativeBuildInputs = with pkgs; [ - makeWrapper - # wifi plugin - # cargo networkmanager dependency - pkgconf - dbus - ]; - - buildInputs = with pkgs; [ dbus ]; - - cargoTOML = builtins.fromTOML (builtins.readFile (./. + "/Cargo.toml")); - - inherit (cargoTOML.workspace.package) version; - pname = "centerpiece"; - - craneLib = crane.mkLib nixpkgs.legacyPackages.${system}; - fontFilter = path: _type: builtins.match ".*ttf$" path != null; - configFilter = path: _type: builtins.match ".*config.yml$" path != null; - assetOrCargo = - path: type: - (configFilter path type) || (fontFilter path type) || (craneLib.filterCargoSources path type); - commonArgs = { - src = pkgs.lib.cleanSourceWith { - src = craneLib.path ./.; - filter = assetOrCargo; - }; - inherit - pname - version - buildInputs - nativeBuildInputs - ; - }; - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - cargoClippy = craneLib.cargoClippy ( - commonArgs - // { - inherit cargoArtifacts; - cargoClippyExtraArgs = "--all-targets --all-features"; - } - ); - GIT_DATE = "${builtins.substring 0 4 self.lastModifiedDate}-${ - builtins.substring 4 2 self.lastModifiedDate - }-${builtins.substring 6 2 self.lastModifiedDate}"; - GIT_REV = self.shortRev or "Not committed yet."; - treefmt = (treefmt-nix.lib.evalModule pkgs ./formatter.nix).config.build; - libPath = pkgs.lib.makeLibraryPath [ - pkgs.wayland - pkgs.libxkbcommon - pkgs.vulkan-loader - pkgs.libGL - ]; - - in - { - devShells.${system}.default = pkgs.mkShell { - inherit - nativeBuildInputs - buildInputs - GIT_DATE - GIT_REV - ; - packages = devInputs ++ [ treefmt.wrapper ]; - LD_LIBRARY_PATH = libPath; - }; - packages.${system} = { - default = craneLib.buildPackage ( - commonArgs - // { - inherit - cargoArtifacts - nativeBuildInputs - buildInputs - pname - GIT_REV - GIT_DATE - ; - postFixup = pkgs.lib.optional pkgs.stdenv.isLinux '' - rpath=$(patchelf --print-rpath $out/bin/${pname}) - patchelf --set-rpath "$rpath:${libPath}" $out/bin/${pname} - ''; - - meta = with pkgs.lib; { - description = "Your trusty omnibox search."; - homepage = "https://github.com/friedow/centerpiece"; - platforms = platforms.linux; - license = licenses.mit; - mainProgram = pname; - maintainers = [ "friedow" ]; - }; - } - ); - index-git-repositories = craneLib.buildPackage ( - commonArgs - // rec { - inherit cargoArtifacts; - pname = "index-git-repositories"; - cargoExtraArgs = "-p ${pname}"; - meta.mainProgram = pname; - } - ); - }; - checks.${system} = { - inherit (self.outputs.packages.${system}) default index-git-repositories; - shell = self.outputs.devShells.${system}.default; - treefmt = treefmt.check self; - inherit cargoClippy; - hmModule = - (nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - modules = [ - home-manager.nixosModules.home-manager - { - home-manager.users.alice = { - imports = [ self.outputs.hmModules."x86_64-linux".default ]; - programs.centerpiece = { - enable = true; - config.plugin.git_repositories.commands = [ [ "alacritty" ] ]; - services.index-git-repositories = { - enable = true; - interval = "3hours"; - }; - }; - home.stateVersion = "23.11"; - }; - users.users.alice = { - isNormalUser = true; - uid = 1000; - home = "/home/alice"; - }; - } - ]; - }).config.system.build.vm; - }; - hmModules.${system}.default = import ./home-manager-module.nix { - centerpiece = self.outputs.packages.${system}.default; - inherit (self.outputs.packages.${system}) index-git-repositories; - }; - formatter.${system} = treefmt.wrapper; - }; + outputs = args: import ./nix args; } diff --git a/formatter.nix b/formatter.nix deleted file mode 100644 index 5a4a512..0000000 --- a/formatter.nix +++ /dev/null @@ -1,12 +0,0 @@ -_: { - projectRootFile = "flake.nix"; - - programs = { - deadnix.enable = true; - nixfmt.enable = true; - rustfmt.enable = true; - statix.enable = true; - taplo.enable = true; - yamlfmt.enable = true; - }; -} diff --git a/nix/checks.nix b/nix/checks.nix new file mode 100644 index 0000000..595dcc2 --- /dev/null +++ b/nix/checks.nix @@ -0,0 +1,16 @@ +{ self, ... }: +{ + perSystem = + { pkgs, ... }: + { + checks = { + inherit ((pkgs.callPackage ./crane.nix { inherit self; })) + centerpiece + index-git-repositories + cargoArtifacts + cargoClippy + cargoTest + ; + }; + }; +} diff --git a/nix/crane.nix b/nix/crane.nix new file mode 100644 index 0000000..d0762f0 --- /dev/null +++ b/nix/crane.nix @@ -0,0 +1,83 @@ +{ + self, + lib, + pkgs, + makeWrapper, + pkgconf, + dbus, +}: +let + cargoTOML = builtins.fromTOML (builtins.readFile (self + "/Cargo.toml")); + inherit (cargoTOML.workspace.package) version; + pname = "centerpiece"; + meta = import ./meta.nix { inherit lib; }; + craneLib = self.inputs.crane.mkLib pkgs; + commonArgs = { + nativeBuildInputs = [ + makeWrapper + # wifi plugin + # cargo networkmanager dependency + pkgconf + dbus + ]; + buildInputs = [ dbus ]; + inherit + meta + version + pname + ; + src = + let + fontFilter = path: _type: builtins.match ".*ttf$" path != null; + configFilter = path: _type: builtins.match ".*config.yml$" path != null; + assetOrCargo = + path: type: + (configFilter path type) || (fontFilter path type) || (craneLib.filterCargoSources path type); + in + lib.cleanSourceWith { + src = craneLib.path ../.; + filter = assetOrCargo; + }; + }; + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + cargoClippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets --all-features"; + } + ); + cargoTest = craneLib.cargoNextest (commonArgs // { inherit cargoArtifacts; }); +in +{ + centerpiece = craneLib.buildPackage ( + commonArgs + // { + env = import ./env.nix { inherit self; }; + postFixup = pkgs.lib.optional pkgs.stdenv.isLinux '' + rpath=$(patchelf --print-rpath $out/bin/${pname}) + patchelf --set-rpath "$rpath:${pkgs.callPackage ./lib.nix { }}" $out/bin/${pname} + ''; + doCheck = false; + inherit + cargoArtifacts + ; + } + ); + index-git-repositories = craneLib.buildPackage ( + commonArgs + // { + cargoExtraArgs = "-p index-git-repositories"; + pname = "index-git-repositories"; + doCheck = false; + inherit + cargoArtifacts + ; + } + ); + inherit + cargoClippy + cargoArtifacts + cargoTest + ; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..ab39f09 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,15 @@ +{ flake-parts, ... }@inputs: +flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + + imports = [ + ./devshells.nix + ./formatter.nix + ./packages.nix + ./checks.nix + ]; + +} diff --git a/nix/devshells.nix b/nix/devshells.nix new file mode 100644 index 0000000..7140936 --- /dev/null +++ b/nix/devshells.nix @@ -0,0 +1,19 @@ +{ self, ... }: +{ + perSystem = + { pkgs, self', ... }: + { + devShells = { + default = pkgs.mkShellNoCC { + name = "centerpiece"; + inputsFrom = [ self'.packages.default ]; + packages = [ + pkgs.clippy + self'.formatter.outPath + ]; + env = import ./env.nix { inherit self; }; + LD_LIBRARY_PATH = pkgs.callPackage ./lib.nix { }; + }; + }; + }; +} diff --git a/nix/env.nix b/nix/env.nix new file mode 100644 index 0000000..e40403b --- /dev/null +++ b/nix/env.nix @@ -0,0 +1,7 @@ +{ self, ... }: +{ + GIT_DATE = "${builtins.substring 0 4 self.lastModifiedDate}-${ + builtins.substring 4 2 self.lastModifiedDate + }-${builtins.substring 6 2 self.lastModifiedDate}"; + GIT_REV = self.shortRev or self.dirtyShortRev; +} diff --git a/nix/formatter.nix b/nix/formatter.nix new file mode 100644 index 0000000..134c063 --- /dev/null +++ b/nix/formatter.nix @@ -0,0 +1,19 @@ +{ inputs, ... }: +{ + imports = [ inputs.treefmt-nix.flakeModule ]; + + perSystem = _: { + treefmt = { + projectRootFile = ".git/config"; + + programs = { + deadnix.enable = true; + nixfmt.enable = true; + rustfmt.enable = true; + statix.enable = true; + taplo.enable = true; + yamlfmt.enable = true; + }; + }; + }; +} diff --git a/home-manager-module.nix b/nix/home-manager-module.nix similarity index 100% rename from home-manager-module.nix rename to nix/home-manager-module.nix diff --git a/nix/lib.nix b/nix/lib.nix new file mode 100644 index 0000000..a2e62df --- /dev/null +++ b/nix/lib.nix @@ -0,0 +1,10 @@ +{ + pkgs, + ... +}: +pkgs.lib.makeLibraryPath [ + pkgs.wayland + pkgs.libxkbcommon + pkgs.vulkan-loader + pkgs.libGL +] diff --git a/nix/meta.nix b/nix/meta.nix new file mode 100644 index 0000000..96a31cb --- /dev/null +++ b/nix/meta.nix @@ -0,0 +1,9 @@ +{ lib, ... }: +{ + description = "Your trusty omnibox search."; + homepage = "https://github.com/friedow/centerpiece"; + mainProgram = "centerpiece"; + platforms = lib.platforms.linux; + license = [ lib.licenses.mit ]; + maintainers = [ "friedow" ]; +} diff --git a/nix/packages.nix b/nix/packages.nix new file mode 100644 index 0000000..d24ed8c --- /dev/null +++ b/nix/packages.nix @@ -0,0 +1,13 @@ +_: { + perSystem = + { self', ... }: + { + packages = rec { + default = centerpiece; + inherit (self'.checks) + centerpiece + index-git-repositories + ; + }; + }; +}