From cdc4b7d03efa423ca252f26bd92e57279e41813d Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sat, 25 Apr 2026 19:29:13 +0900 Subject: [PATCH] nix/package: init --- flake.lock | 16 ++++++++++ flake.nix | 30 ++++++++++++++++- nix/package.nix | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 nix/package.nix diff --git a/flake.lock b/flake.lock index cbcf50b..65a7ced 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1776635034, + "narHash": "sha256-OEOJrT3ZfwbChzODfIH4GzlNTtOFuZFWPtW7jIeR8xU=", + "owner": "ipetkov", + "repo": "crane", + "rev": "dc7496d8ea6e526b1254b55d09b966e94673750f", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1776877367, @@ -18,6 +33,7 @@ }, "root": { "inputs": { + "crane": "crane", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay" } diff --git a/flake.nix b/flake.nix index b41fd2a..15002ce 100644 --- a/flake.nix +++ b/flake.nix @@ -4,9 +4,11 @@ rust-overlay.url = "github:oxalica/rust-overlay"; rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; + + crane.url = "github:ipetkov/crane"; }; - outputs = { self, nixpkgs, rust-overlay }: + outputs = { self, nixpkgs, rust-overlay, crane }: let inherit (nixpkgs) lib; @@ -31,6 +33,32 @@ }; in f system pkgs toolchain); in { + apps = forAllSystems (system: _: _: { + default = self.apps.${system}.kagami; + kagami = { + type = "app"; + program = lib.getExe self.packages.${system}.kagami; + }; + }); + + packages = forAllSystems (system: pkgs: _: { + default = self.packages.${system}.kagami; + kagami = let + cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); + cargoLock = ./Cargo.lock; + craneLib = crane.mkLib pkgs; + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + (craneLib.fileset.commonCargoSources ./.) + ]; + }; + in pkgs.callPackage ./nix/package.nix { + useCrane = true; + inherit cargoToml cargoLock src craneLib; + }; + }); + devShells = forAllSystems (system: pkgs: toolchain: { default = pkgs.mkShell { nativeBuildInputs = with pkgs; [ diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..dbaf933 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,85 @@ +{ + lib +, rustPlatform +, stdenv +, buildPackages +, installShellFiles +, versionCheckHook +, pkg-config +, openssl + +, cargoToml +, cargoLock +, src + +, useCrane ? false +, craneLib ? null +}: +let + mainProgram = (lib.head cargoToml.bin).name; + buildFunction = if useCrane then craneLib.buildPackage else rustPlatform.buildRustPackage; + + pnameCraneSuffix = lib.optionalString useCrane "-crane"; + pname = "${cargoToml.package.name}${pnameCraneSuffix}"; + + rustPlatformArgs = { + buildType = "release-lto"; + cargoLock.lockFile = cargoLock; + + doCheck = true; + useNextest = true; + nativeCheckInputs = [ + versionCheckHook + ]; + }; + + craneArgs = { + cargoLock = cargoLock; + cargoArtifacts = craneLib.buildDepsOnly { + inherit pname; + inherit (cargoToml.package) version; + src = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + (craneLib.fileset.cargoTomlAndLock ../.) + ]; + }; + + nativeBuildInputs = [ pkg-config ]; + buildInputs = [ openssl ]; + + cargoLock = cargoLock; + }; + }; +in +buildFunction ({ + inherit pname; + inherit (cargoToml.package) version; + inherit src; + + nativeBuildInputs = [ + installShellFiles + pkg-config + ]; + buildInputs = [ + openssl + ]; + postInstall = let + emulatorAvailable = stdenv.hostPlatform.emulatorAvailable buildPackages; + emulator = stdenv.hostPlatform.emulator buildPackages; + in lib.optionalString emulatorAvailable '' + installShellCompletion --cmd kagami \ + --bash <(${emulator} $out/bin/kagami generate-completions --shell bash) \ + --fish <(${emulator} $out/bin/kagami generate-completions --shell fish) \ + --zsh <(${emulator} $out/bin/kagami generate-completions --shell zsh) + ''; + + meta = with lib; { + license = licenses.bsd3; + platforms = platforms.unix ++ platforms.windows; + inherit mainProgram; + }; +} +// +(if useCrane then craneArgs else rustPlatformArgs) +)