Skip to content

Instantly share code, notes, and snippets.

@shanesveller
Created December 1, 2021 15:45
Show Gist options
  • Save shanesveller/50762fc49fd52b5fee1e8239f01f93b8 to your computer and use it in GitHub Desktop.
Save shanesveller/50762fc49fd52b5fee1e8239f01f93b8 to your computer and use it in GitHub Desktop.
Rust + Axum development environment
{
"nodes": {
"cargo2nix": {
"inputs": {
"flake-utils": [
"flake-utils"
],
"nixpkgs": [
"nixpkgs"
],
"rust-overlay": [
"rust-overlay"
]
},
"locked": {
"lastModified": 1637398005,
"narHash": "sha256-m5XfJAW81tKjXb7ci22MieRMTypizyaNZVHjxKF6V64=",
"owner": "cargo2nix",
"repo": "cargo2nix",
"rev": "cf339618f8594ef1d83940a8c3ce37850b6bfc58",
"type": "github"
},
"original": {
"owner": "cargo2nix",
"repo": "cargo2nix",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1627913399,
"narHash": "sha256-hY8g6H2KFL8ownSiFeMOjwPC8P0ueXpCVEbxgda3pko=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "12c64ca55c1014cdc1b16ed5a804aa8576601ff2",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1638122382,
"narHash": "sha256-sQzZzAbvKEqN9s0bzWuYmRaA03v40gaJ4+iL1LXjaeI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "74f7e4319258e287b0f9cb95426c9853b282730b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"master": {
"locked": {
"lastModified": 1638133678,
"narHash": "sha256-iYpfV+O1RwFZvDBpWgNDO5JfSOTCe456j457L22JYs0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "fb41277df396a390a08a3b3afa57f122df618d4b",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "master",
"type": "indirect"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1637448181,
"narHash": "sha256-ujcXli4esmtIHUBjE1BjmMuBWrcNvlHZrVXx56i5B1M=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d5b65f812cd4f5a8fa74b406075b59a46f1cfd98",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-21.05",
"type": "indirect"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-utils": [
"flake-utils"
],
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1637745948,
"narHash": "sha256-DmQG1bZk24eS+BAHwnHPyYIadMLKbq0d1b//iapYIPU=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "c3b4f94350b0e59c2546fa85890cc70d03616b9c",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"cargo2nix": "cargo2nix",
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"master": "master",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks",
"rust-overlay": "rust-overlay",
"unstable": "unstable"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": [
"flake-utils"
],
"nixpkgs": [
"unstable"
]
},
"locked": {
"lastModified": 1638065687,
"narHash": "sha256-T8s/mgeS74Qtk48HEnZiQxpleekJP6rFPgkffG7imBc=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "1cd4ebce6b21f9aa38bfa3d5021135ea5568797f",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"unstable": {
"locked": {
"lastModified": 1637753138,
"narHash": "sha256-yo7E4a/miP77fH3Ksp9tmYECAPjxBeweJFeZ4tX9O/U=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "39cb89ef2ffcbef8d2ac2d52267ac69121a6becb",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-unstable",
"type": "indirect"
}
}
},
"root": "root",
"version": 7
}
{
description = "Rust + Axum development environment";
inputs = {
cargo2nix.url = "github:cargo2nix/cargo2nix";
cargo2nix.inputs.flake-compat.follows = "flake-compat";
cargo2nix.inputs.flake-utils.follows = "flake-utils";
cargo2nix.inputs.nixpkgs.follows = "nixpkgs";
cargo2nix.inputs.rust-overlay.follows = "rust-overlay";
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
flake-utils.url = "github:numtide/flake-utils";
master.url = "nixpkgs/master";
nixpkgs.url = "nixpkgs/nixos-21.05";
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.flake-utils.follows = "flake-utils";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.flake-utils.follows = "flake-utils";
rust-overlay.inputs.nixpkgs.follows = "unstable";
unstable.url = "nixpkgs/nixos-unstable";
};
outputs = inputs@{ self, flake-utils, nixpkgs, ... }:
flake-utils.lib.eachSystem [ "x86_64-darwin" "x86_64-linux" ] (system:
let
master = import inputs.master { inherit system; };
unstable = import inputs.unstable { inherit system; };
pkgs = import nixpkgs {
inherit system;
overlays = inputs.cargo2nix.overlays."${system}"
++ [ (final: prev: { inherit master unstable; }) ];
};
sharedInputs = with pkgs;
[
cargo-asm
cargo-audit
cargo-bloat
cargo-cache
cargo-deny
cargo-edit
cargo-expand
cargo-flamegraph
cargo-generate
cargo-geiger
cargo-make
self.packages."${system}".cargo-outdated
cargo-release
cargo-sweep
cargo-udeps
cargo-watch
cargo-whatfeatures
clang
just
lld
mdbook
# Out-of-order intentional for PATH priority
self.packages."${system}".rust-analyzer
self.packages."${system}".cargo2nix
self.packages."${system}".sqlx-cli
# rustEnv
self.packages."${system}".sccache
] ++ lib.optionals (stdenv.isLinux) [
cargo-tarpaulin
perf-tools
strace
valgrind
];
rustChannel =
pkgs.lib.removeSuffix "\n" (builtins.readFile ./rust-toolchain);
rustTools = pkgs.rust-bin.stable.${rustChannel};
rustPkgs = pkgs.rustBuilder.makePackageSet' {
inherit rustChannel;
packageFun = import ./Cargo.nix;
packageOverrides = pkgs:
pkgs.rustBuilder.overrides.all ++ [
(pkgs.rustBuilder.rustLib.makeOverride {
name = "some_project_core";
overrideAttrs = _drv: { SQLX_OFFLINE = "true"; };
})
];
rootFeatures = [
"some_project_cli/default"
"some_project_core/default"
"some_project_core/otel"
"some_project_web/default"
"some_project_web/otel"
];
};
in {
checks = {
pre-commit-check = inputs.pre-commit-hooks.lib."${system}".run {
src = ./.;
hooks = {
cargo-check.enable = true;
clippy.enable = true;
nix-linter.enable = true;
nix-linter.excludes = [ "Cargo.nix" ];
nixfmt.enable = true;
prettier.enable = true;
rustfmt.enable = true;
shellcheck.enable = true;
cargo2nix = {
enable = true;
name = "regenerate Cargo.nix";
entry =
"${self.packages."${system}".cargo2nix}/bin/cargo2nix -f";
files = "Cargo.lock";
language = "system";
pass_filenames = false;
};
};
tools = {
inherit (rustTools) cargo rustFmt;
inherit (pkgs.unstable) nodePackages;
inherit (self.packages."${system}") clippy;
};
};
};
devShell = pkgs.mkShell {
inherit (self.checks."${system}".pre-commit-check) shellHook;
inputsFrom = pkgs.lib.mapAttrsToList (_: pkg: pkg { })
rustPkgs.noBuild.workspace;
nativeBuildInputs = [ rustPkgs.rustChannel ] ++ sharedInputs;
NIX_PATH =
"nixpkgs=${nixpkgs}:unstable=${inputs.unstable}:master=${inputs.master}";
PROTOC = "${pkgs.protobuf}/bin/protoc";
PROTOC_INCLUDE = "${pkgs.protobuf}/include";
RUSTC_WRAPPER = "${self.packages."${system}".sccache}/bin/sccache";
RUST_SRC_PATH =
"${rustPkgs.rustChannel}/lib/rustlib/src/rust/library";
};
packages = {
inherit (pkgs.master) rust-analyzer sqlx-cli;
inherit (pkgs.unstable) sccache;
cargo2nix = inputs.cargo2nix.defaultPackage."${system}";
cargo-outdated = pkgs.symlinkJoin {
name = "cargo-outdated";
paths = [ pkgs.cargo-outdated ];
buildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/cargo-outdated \
--unset RUST_LOG
'';
};
clippy = pkgs.symlinkJoin {
name = "clippy";
paths = [ pkgs.clang rustTools.clippy pkgs.lld ];
buildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/cargo-clippy \
--prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.clang pkgs.lld ]}
'';
};
docker = pkgs.dockerTools.streamLayeredImage {
name = "some_project";
tag = "latest";
contents = with self.packages.x86_64-linux; [
some_project_web
some_project_cli
];
config = {
Cmd =
[ "${self.packages.x86_64-linux.some_project_web}/bin/api" ];
Env = [ "RUST_LOG=debug" ];
};
};
some_project_cli = (rustPkgs.workspace.some_project_cli { }).bin;
some_project_web = (rustPkgs.workspace.some_project_web { }).bin;
gcroot = pkgs.linkFarmFromDrvs "some_project"
(with self.outputs; [ devShell."${system}".inputDerivation ]);
nightlyDevShell = pkgs.mkShell {
inputsFrom = pkgs.lib.mapAttrsToList (_: pkg: pkg { })
rustPkgs.noBuild.workspace;
nativeBuildInputs = [ pkgs.rust-bin.nightly.latest.default ]
++ sharedInputs;
RUSTFLAGS = "-Z macro-backtrace";
};
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment