nix-samples/flake-rust-docker/flake.nix

72 lines
2.3 KiB
Nix

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils = {
url = "github:numtide/flake-utils";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
crane = {
url = "github:ipetkov/crane";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }:
flake-utils.lib.eachDefaultSystem
(system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# this is how we can tell crane to use our toolchain!
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
# cf. https://crane.dev/API.html#libcleancargosource
src = craneLib.cleanCargoSource ./hello-world;
# as before
nativeBuildInputs = with pkgs; [ rustToolchain pkg-config ];
buildInputs = with pkgs; [ openssl sqlite ];
# because we'll use it for both `cargoArtifacts` and `bin`
commonArgs = {
inherit src buildInputs nativeBuildInputs;
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# remember, `set1 // set2` does a shallow merge:
bin = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
});
dockerImage = pkgs.dockerTools.buildImage {
name = "hello-world";
tag = "latest";
copyToRoot = [ bin ];
config = {
Cmd = [ "${bin}/bin/hello-world" ];
};
};
in
with pkgs;
{
packages =
{
# that way we can build `bin` specifically,
# but it's also the default.
inherit bin dockerImage;
default = bin;
};
devShells.default = mkShell {
# instead of passing `buildInputs` / `nativeBuildInputs`,
# we refer to an existing derivation here
inputsFrom = [ bin ];
buildInputs = with pkgs; [ dive docker shadow ];
};
}
);
}