Skip to content

Commit

Permalink
chore: Nixify local devshell!
Browse files Browse the repository at this point in the history
  • Loading branch information
x10an14-nav committed Jul 14, 2023
1 parent 959c25c commit 533bf0b
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
/target
.idea/

.env*

# Nix stuff
.direnv/
result*
151 changes: 151 additions & 0 deletions flake.lock

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

160 changes: 160 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
description = "A Nix-flake-based Rust development environment";

inputs = {
crane = {
url = "github:ipetkov/crane";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};

outputs = {
self,
crane,
flake-utils,
nixpkgs,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};

# # Target musl when building on 64-bit linux
# buildTarget =
# {"x86_64-linux" = "x86_64-unknown-linux-musl";}.${system}
# or (pkgs.rust.toRustTargetSpec pkgs.stdenv.hostPlatform);
# rustToolchain = pkgs.rust-bin.stable.latest.default.override {
# targets = [
# buildTarget
# (pkgs.rust.toRustTargetSpec pkgs.stdenv.hostPlatform)
# ];
# };
rustToolchain = pkgs.rust-bin.stable.latest.default;

# Set-up build dependencies and configure rust
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;

inherit (pkgs) lib;
# Shamelessly stolen from:
# https://github.com/fedimint/fedimint/blob/66519d5e978e22bb10a045f406101891e1bb7eb5/flake.nix#L99
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;
};

cargo-details = lib.importTOML ./Cargo.toml;
pname = cargo-details.package.name;
commonArgs = {
nativeBuildInputs = with pkgs; [
glib
openssl
pkg-config
];
# CARGO_BUILD_TARGET = buildTarget;
};

# Compile and cache only cargo dependencies
dep-files-filter = ["Cargo.lock" "Cargo.toml" ".*/Cargo.toml"];
cargo-deps = craneLib.buildDepsOnly (commonArgs
// {
src = filterSrcWithRegexes dep-files-filter ./.;
inherit pname;
});

# Compile and cache only workspace code (seperately from 3rc party dependencies)
package-file-filter =
dep-files-filter
++ [
".*\.rs"
];
cargo-package = craneLib.buildPackage (commonArgs
// {
inherit cargo-deps pname;
src = filterSrcWithRegexes package-file-filter ./.;
});
in {
checks = {
# inherit # Comment in when you want tests to run on every new shell
# cargo-package
# ;
};
devShells.default = pkgs.mkShell {
packages =
(with pkgs; [
# rust specific
cargo-audit
cargo-auditable
cargo-cross
cargo-deny
cargo-outdated
rust-analyzer

# Editor stuffs
helix
lldb
rust-analyzer

# Nix stuff
nix-output-monitor
])
++ commonArgs.nativeBuildInputs
++ [
# Packages made in this flake
rustToolchain
# cargo-package # Comment in when you want tests to run on every new shell
]
++ lib.optionals (pkgs.stdenv.isLinux) (with pkgs; [cargo-watch]); # Currently broken on macOS

shellHook = ''
${rustToolchain}/bin/cargo --version
${pkgs.helix}/bin/hx --version
${pkgs.helix}/bin/hx --health rust
'';
};
packages = {
rust = cargo-package;
docker = pkgs.dockerTools.buildImage {
name = pname;
tag = "v${cargo-details.package.version}";
extraCommands = ''mkdir -p data'';
config = {
Cmd = "--help";
Entrypoint = ["${cargo-package}/bin/${pname}"];
};
};
};
packages.default = cargo-package;

# Now `nix fmt` works!
formatter = pkgs.nixfmt;
});
}

0 comments on commit 533bf0b

Please sign in to comment.