Skip to content

Commit

Permalink
flake: rudimentary crane based package definitions
Browse files Browse the repository at this point in the history
the checks are limited, nextest only runs the unit tests without
--all-features, because the integration tests require bitcoind and
redis. unfortunately redis is made available via a docker container, and
running docker in the nix sandbox is not possible
  • Loading branch information
nothingmuch committed Jan 3, 2025
1 parent f8b69d2 commit 08a3805
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 16 deletions.
16 changes: 16 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 110 additions & 16 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane";
};

outputs = {
self,
nixpkgs,
flake-utils,
rust-overlay,
crane,
}:
flake-utils.lib.eachDefaultSystem (
system: let
Expand All @@ -35,19 +37,72 @@
nightly = nightly.latest.default;
};

mkShell = rust-bin:
pkgs.mkShell {
# Use crane to define nix packages for the workspace crate
# based on https://crane.dev/examples/quick-start-workspace.html
# default to nightly rust toolchain in crane, mainly due to rustfmt difference
craneLibVersions = builtins.mapAttrs (name: rust-bin: (crane.mkLib pkgs).overrideToolchain (_: rust-bin)) rustVersions;
craneLib = craneLibVersions.nightly;
src = craneLib.cleanCargoSource ./.;
commonArgs = {
inherit src;
strictDeps = true;

# provide fallback name & version for workspace related derivations
# this is mainly to silence warnings from crane about providing a stub
# value overridden in per-crate packages with info from Cargo.toml
pname = "workspace";
version = "no-version";

# default to recent dependency versions
# TODO add overrides for minimal lockfile, once #454 is resolved
cargoLock = ./Cargo-recent.lock;

# tell bitcoind crate not to try to download during build
BITCOIND_SKIP_DOWNLOAD = 1;
};

cargoArtifacts = craneLib.buildDepsOnly commonArgs;
individualCrateArgs =
commonArgs
// {
inherit cargoArtifacts;
doCheck = false; # skip testing, since that's done in flake check
};

fileSetForCrate = subdir:
pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [
./Cargo.toml
(craneLib.fileset.commonCargoSources subdir)
];
};

packages =
builtins.mapAttrs (
name: extraArgs:
craneLib.buildPackage (individualCrateArgs
// craneLib.crateNameFromCargoToml {cargoToml = builtins.toPath "${./.}/${name}/Cargo.toml";}
// {
cargoExtraArgs = "--locked -p ${name} ${extraArgs}";
inherit src;
})
) {
"payjoin" = "--features v2,send,receive";
"payjoin-cli" = "--features v1,v2";
"payjoin-directory" = "";
};

devShells = builtins.mapAttrs (_name: craneLib:
craneLib.devShell {
packages = with pkgs; [
(rust-bin.override {
extensions = ["rust-src" "rustfmt"];
})
cargo-edit
cargo-nextest
cargo-watch
rust-analyzer
];
};
devShells = builtins.mapAttrs (_name: rustVersion: mkShell rustVersion) rustVersions;
})
craneLibVersions;

simpleCheck = args:
pkgs.stdenvNoCC.mkDerivation ({
Expand All @@ -57,18 +112,57 @@
}
// args);
in {
packages = packages;
devShells = devShells // {default = devShells.nightly;};
formatter = pkgs.alejandra;
checks = {
nix-fmt-check = simpleCheck {
name = "nix-fmt-check";
src = pkgs.lib.sources.sourceFilesBySuffices ./. [".nix"];
nativeBuildInputs = [pkgs.alejandra];
checkPhase = ''
alejandra -c .
'';
checks =
packages
// {
payjoin-workspace-nextest = craneLib.cargoNextest (commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
# TODO also run integration tests
# this needs --all-features to enable io,_danger_local_https features
# unfortunately this can't yet work because running docker inside the nix sandbox is not possible,
# which precludes use of the redis test container
# cargoExtraArgs = "--locked --all-features";
# buildInputs = [ pkgs.bitcoind ]; # not verified to work
});

payjoin-workspace-nextest-msrv = craneLibVersions.msrv.cargoNextest (commonArgs
// {
cargoArtifacts = craneLibVersions.msrv.buildDepsOnly commonArgs;
partitions = 1;
partitionType = "count";
});

payjoin-workspace-clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --all-features --keep-going -- --deny warnings";
});

payjoin-workspace-doc = craneLib.cargoDoc (commonArgs
// {
inherit cargoArtifacts;
});

payjoin-workspace-fmt = craneLib.cargoFmt (commonArgs
// {
inherit src;
});

nix-fmt-check = simpleCheck {
name = "nix-fmt-check";
src = pkgs.lib.sources.sourceFilesBySuffices ./. [".nix"];
nativeBuildInputs = [pkgs.alejandra];
checkPhase = ''
alejandra -c .
'';
};
};
};
}
);
}

0 comments on commit 08a3805

Please sign in to comment.