From e7091af96110b8d4d150cfceeaab6cd544a2cdcf Mon Sep 17 00:00:00 2001 From: okjodom Date: Mon, 6 May 2024 17:13:08 +0300 Subject: [PATCH] feat: build fedimint-clientd oci images --- flake.nix | 104 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 27 deletions(-) diff --git a/flake.nix b/flake.nix index 1033f88..787ce31 100644 --- a/flake.nix +++ b/flake.nix @@ -16,7 +16,7 @@ inputs.fenix.follows = "fenix"; }; - flake-utils.url = "github:numtide/flake-utils"; + flake-utils = { url = "github:numtide/flake-utils"; }; fedimint = { url = @@ -33,33 +33,27 @@ }; lib = pkgs.lib; flakeboxLib = flakebox.lib.${system} { }; - rustSrc = flakeboxLib.filterSubPaths { - root = builtins.path { - name = "fedimint-clientd"; - path = ./.; - }; - paths = [ "Cargo.toml" "Cargo.lock" ".cargo" "src" ]; - }; - toolchainArgs = let llvmPackages = pkgs.llvmPackages_11; - in { - extraRustFlags = "--cfg tokio_unstable"; + toolchainArgs = + let llvmPackages = pkgs.llvmPackages_11; + in { + extraRustFlags = "--cfg tokio_unstable"; - components = [ "rustc" "cargo" "clippy" "rust-analyzer" "rust-src" ]; + components = [ "rustc" "cargo" "clippy" "rust-analyzer" "rust-src" ]; - args = { - nativeBuildInputs = - [ pkgs.wasm-bindgen-cli pkgs.geckodriver pkgs.wasm-pack ] - ++ lib.optionals (!pkgs.stdenv.isDarwin) [ pkgs.firefox ]; + args = { + nativeBuildInputs = + [ pkgs.wasm-bindgen-cli pkgs.geckodriver pkgs.wasm-pack ] + ++ lib.optionals (!pkgs.stdenv.isDarwin) [ pkgs.firefox ]; + }; + } // lib.optionalAttrs pkgs.stdenv.isDarwin { + # on Darwin newest stdenv doesn't seem to work + # linking rocksdb + stdenv = pkgs.clang11Stdenv; + clang = llvmPackages.clang; + libclang = llvmPackages.libclang.lib; + clang-unwrapped = llvmPackages.clang-unwrapped; }; - } // lib.optionalAttrs pkgs.stdenv.isDarwin { - # on Darwin newest stdenv doesn't seem to work - # linking rocksdb - stdenv = pkgs.clang11Stdenv; - clang = llvmPackages.clang; - libclang = llvmPackages.libclang.lib; - clang-unwrapped = llvmPackages.clang-unwrapped; - }; # all standard toolchains provided by flakebox toolchainsStd = flakeboxLib.mkStdFenixToolchains toolchainArgs; @@ -74,24 +68,80 @@ [ pkgs.darwin.apple_sdk.frameworks.SystemConfiguration ]; nativeBuildInputs = [ pkgs.pkg-config ]; }; + + commonSrc = builtins.path { path = ./.; name = "fedimint-clientd"; }; + + filterWorkspaceDepsBuildFilesRegex = [ "Cargo.lock" "Cargo.toml" ".cargo" ".cargo/.*" ".config" ".config/.*" ".*/Cargo.toml" ]; + + filterSrcWithRegexes = regexes: src: + let + basePath = toString src + "/"; + in + lib.cleanSourceWith { + filter = (path: type: + let + relPath = lib.removePrefix basePath (toString path); + includePath = + (type == "directory") || + lib.any + (re: builtins.match re relPath != null) + regexes; + in + # uncomment to debug: + # builtins.trace "${relPath}: ${lib.boolToString includePath}" + includePath + ); + inherit src; + }; + + # Filter only files needed to build project dependencies + # + # To get good build times it's vitally important to not have to + # rebuild derivation needlessly. The way Nix caches things + # is very simple: if any input file changed, derivation needs to + # be rebuild. + # + # For this reason this filter function strips the `src` from + # any files that are not relevant to the build. + # + # Like `filterWorkspaceFiles` but doesn't even need *.rs files + # (because they are not used for building dependencies) + filterWorkspaceDepsBuildFiles = src: filterSrcWithRegexes filterWorkspaceDepsBuildFilesRegex src; + + + # Filter only files relevant to building the workspace + filterWorkspaceBuildFiles = src: filterSrcWithRegexes (filterWorkspaceDepsBuildFilesRegex ++ [ ".*\.rs" ]) src; + outputs = (flakeboxLib.craneMultiBuild { toolchains = toolchainsStd; }) (craneLib': let craneLib = (craneLib'.overrideArgs { pname = "flexbox-multibuild"; - src = rustSrc; + src = filterWorkspaceBuildFiles commonSrc; }).overrideArgs commonArgs; - in rec { + in + rec { workspaceDeps = craneLib.buildWorkspaceDepsOnly { }; + workspaceBuild = craneLib.buildWorkspace { cargoArtifacts = workspaceDeps; }; + fedimint-clientd = craneLib.buildPackageGroup { pname = "fedimint-clientd"; packages = [ "fedimint-clientd" ]; mainProgram = "fedimint-clientd"; }; + + fedimint-clientd-oci = pkgs.dockerTools.buildLayeredImage { + name = "fedimint-clientd"; + contents = [ fedimint-clientd ]; + config = { + Cmd = [ "${fedimint-clientd}/bin/fedimint-clientd" ]; + }; + }; }); - in { + in + { legacyPackages = outputs; packages = { default = outputs.fedimint-clientd; }; devShells = flakeboxLib.mkShells {