Add a crane build for incremental builds?

This commit is contained in:
Daniel Flanagan 2024-07-31 21:00:21 -05:00
parent 7bf8602796
commit a0289ee546
3 changed files with 114 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/result
/target /target
/.direnv /.direnv
/.pre-commit-config.yaml /.pre-commit-config.yaml

View file

@ -1,5 +1,25 @@
{ {
"nodes": { "nodes": {
"crane": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1721842668,
"narHash": "sha256-k3oiD2z2AAwBFLa4+xfU+7G5fisRXfkvrMTCJrjZzXo=",
"owner": "ipetkov",
"repo": "crane",
"rev": "529c1a0b1f29f0d78fa3086b8f6a134c71ef3aaf",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flake-compat": { "flake-compat": {
"flake": false, "flake": false,
"locked": { "locked": {
@ -94,8 +114,30 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"crane": "crane",
"git-hooks": "git-hooks", "git-hooks": "git-hooks",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1722391647,
"narHash": "sha256-JTi7l1oxnatF1uX/gnGMlRnyFMtylRw4MqhCUdoN2K4=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "0fd4a5d2098faa516a9b83022aec7db766cd1de8",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
} }
} }
}, },

View file

@ -3,10 +3,16 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
git-hooks.url = "github:cachix/git-hooks.nix"; git-hooks.url = "github:cachix/git-hooks.nix";
git-hooks.inputs.nixpkgs.follows = "nixpkgs"; git-hooks.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
crane.inputs.nixpkgs.follows = "nixpkgs";
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
}; };
outputs = { outputs = {
self, self,
git-hooks, git-hooks,
rust-overlay,
crane,
nixpkgs, nixpkgs,
}: let }: let
inherit (self) outputs; inherit (self) outputs;
@ -14,7 +20,7 @@
systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"]; systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"];
forSystems = nixpkgs.lib.genAttrs systems; forSystems = nixpkgs.lib.genAttrs systems;
pkgsFor = system: (import nixpkgs {inherit system;}).extend overlays.default; pkgsFor = system: ((import nixpkgs {inherit system;}).extend overlays.default).extend rust-overlay.overlays.default;
genPkgs = func: (forSystems (system: func (pkgsFor system))); genPkgs = func: (forSystems (system: func (pkgsFor system)));
buildTimeDeps = pkgs: buildTimeDeps = pkgs:
with pkgs; [ with pkgs; [
@ -61,8 +67,68 @@
build = outputs.packages.${pkgs.system}.default; build = outputs.packages.${pkgs.system}.default;
}); });
packages = genPkgs (pkgs: { packages = genPkgs (pkgs: let
kodotag = pkgs.rustPlatform.buildRustPackage { rustToolchainFor = p:
p.rust-bin.selectLatestNightlyWith (toolchain:
toolchain.default.override {
extensions = ["rust-src"];
targets = ["x86_64-unknown-linux-gnu"];
});
rustToolchain = rustToolchainFor pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor;
src = let
# per https://github.com/ipetkov/crane/blob/529c1a0b1f29f0d78fa3086b8f6a134c71ef3aaf/docs/source-filtering.md we need the font file since we bake it into the binary as the default bevy font
fontFilter = path: _type: builtins.match ".*ttf$" path != null;
filteredSources = path: type:
(fontFilter path type) || (craneLib.filterCargoSources path type);
in
pkgs.lib.cleanSourceWith {
src = ./.; # The original, unfiltered source
filter = filteredSources;
name = "source"; # Be reproducible, regardless of the directory name
};
in {
kodotag = craneLib.buildPackage {
# TODO: incremental rust builds with nix would be awesome
pname = "kodotag";
version = "0.1.0";
inherit src;
strictDeps = true;
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles src) cargoConfigs;
cargoLockList = [
./Cargo.lock
# Unfortunately this approach requires IFD (import-from-derivation)
# otherwise Nix will refuse to read the Cargo.lock from our toolchain
# (unless we build with `--impure`).
#
# Another way around this is to manually copy the rustlib `Cargo.lock`
# to the repo and import it with `./path/to/rustlib/Cargo.lock` which
# will avoid IFD entirely but will require manually keeping the file
# up to date!
"${rustToolchain.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock"
];
};
# cargoExtraArgs = "-Z build-std --target x86_64-unknown-linux-gnu";
nativeBuildInputs = buildTimeDeps pkgs;
buildInputs = linkTimeDeps pkgs;
hash = pkgs.lib.fakeHash;
# cargoHash = "";
cargoHash = "sha256-d7luBIWHWn2ZpmSnIgpuLXsy1URUIojpPFboMtqa2ps=";
# a hack to avoid using mold as our linker when building with nix
postUnpack = ''
ls -la
rm -r ./*/.cargo
'';
};
kodotag-no-crane = pkgs.rustPlatform.buildRustPackage {
pname = "kodotag"; pname = "kodotag";
version = "0.1.0"; version = "0.1.0";
nativeBuildInputs = buildTimeDeps pkgs; nativeBuildInputs = buildTimeDeps pkgs;
@ -79,7 +145,7 @@
''; '';
}; };
default = outputs.packages.${pkgs.system}.kodotag; default = outputs.packages.${pkgs.system}.kodotag-no-crane;
}); });
devShells = genPkgs (pkgs: { devShells = genPkgs (pkgs: {