From 3ff8bf71d5babf62235af3582c28547929280fff Mon Sep 17 00:00:00 2001 From: chayleaf Date: Mon, 12 Aug 2024 05:41:21 +0700 Subject: [PATCH] add nix package --- flake.lock | 21 ++++++++++++++++ flake.nix | 70 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/flake.lock b/flake.lock index 8c16bf8..bbf1b5f 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,25 @@ { "nodes": { + "crane": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1722960479, + "narHash": "sha256-NhCkJJQhD5GUib8zN9JrmYGMwt4lCRp6ZVNzIiYCl0Y=", + "owner": "ipetkov", + "repo": "crane", + "rev": "4c6c77920b8d44cd6660c1621dea6b3fc4b4c4f4", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1723151029, @@ -18,6 +38,7 @@ }, "root": { "inputs": { + "crane": "crane", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index af9e93d..5eb126e 100644 --- a/flake.nix +++ b/flake.nix @@ -3,33 +3,57 @@ inputs = { nixpkgs.url = "github:chayleaf/nixpkgs/unbound"; + crane.url = "github:ipetkov/crane"; + crane.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = { self, nixpkgs }: { + outputs = { self, nixpkgs, crane }: let + gen = func: nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" ] (system: func (import nixpkgs { inherit system; })); + in { - packages.x86_64-linux.bindings = let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - in pkgs.unbound-full.overrideAttrs (old: { - name = "unbound-dynmod-bindings.rs"; - nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.rust-bindgen pkgs.rustfmt ]; - phases = ["unpackPhase" "patchPhase" "configurePhase" "installPhase"]; - outputs = [ "out" ]; - installPhase = '' - cp ${./dummy.h} ./dummy.h - bindgen ./dummy.h -- -I $PWD > $out - ''; + packages = gen (pkgs: rec { + bindings = pkgs.unbound-full.overrideAttrs (old: { + name = "unbound-dynmod-bindings.rs"; + nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.rust-bindgen pkgs.rustfmt ]; + phases = ["unpackPhase" "patchPhase" "configurePhase" "installPhase"]; + outputs = [ "out" ]; + installPhase = '' + cp ${./dummy.h} ./dummy.h + bindgen ./dummy.h -- -I $PWD > $out + ''; + }); + unbound-mod = let + craneLib = crane.mkLib pkgs; + inherit (nixpkgs) lib; + in craneLib.buildPackage { + pname = "unbound-mod"; + version = "0.1.0"; + postPatch = '' + cp ${bindings} src/bindings.rs + ''; + src = nixpkgs.lib.cleanSourceWith { + src = ./.; + filter = path: type: lib.hasSuffix ".h" path || craneLib.filterCargoSources path type; + }; + doCheck = false; + LIBMNL_LIB_DIR = "${nixpkgs.lib.getLib pkgs.libmnl}/lib"; + LIBNFTNL_LIB_DIR = "${nixpkgs.lib.getLib (pkgs.libnftnl.overrideAttrs (old: { + patches = (old.patches or []) ++ [ ./libnftnl-fix.patch ]; + }))}/lib"; + }; + default = unbound-mod; }); - devShells.x86_64-linux.default = let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - in pkgs.mkShell rec { - name = "unbound-rust-mod-shell"; - nativeBuildInputs = [ pkgs.rustc pkgs.cargo pkgs.nftables ]; - LIBMNL_LIB_DIR = "${nixpkgs.lib.getLib pkgs.libmnl}/lib"; - LIBNFTNL_LIB_DIR = "${nixpkgs.lib.getLib (pkgs.libnftnl.overrideAttrs (old: { - patches = (old.patches or []) ++ [ ./libnftnl-fix.patch ]; - }))}/lib"; - LD_LIBRARY_PATH = "${LIBMNL_LIB_DIR}:${LIBNFTNL_LIB_DIR}"; - }; + devShells = gen (pkgs: { + default = pkgs.mkShell rec { + name = "unbound-rust-mod-shell"; + nativeBuildInputs = [ pkgs.rustc pkgs.cargo pkgs.nftables ]; + LIBMNL_LIB_DIR = "${nixpkgs.lib.getLib pkgs.libmnl}/lib"; + LIBNFTNL_LIB_DIR = "${nixpkgs.lib.getLib (pkgs.libnftnl.overrideAttrs (old: { + patches = (old.patches or []) ++ [ ./libnftnl-fix.patch ]; + }))}/lib"; + LD_LIBRARY_PATH = "${LIBMNL_LIB_DIR}:${LIBNFTNL_LIB_DIR}"; + }; + }); }; }