Skip to content

Commit

Permalink
Merge pull request #106 from akirak/update-crane
Browse files Browse the repository at this point in the history
Update on Rust Crane
  • Loading branch information
akirak authored Nov 6, 2024
2 parents 7cb2509 + 89b579f commit 96d8779
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 80 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,23 @@ concrete information.

### [rust](rust/)

This flake.nix provides a Rust toolchain.
This is a template for a simple Rust project with a single executable package.

``` bash
nix flake init -t github:akirak/flake-templates#rust
```

It relies on [oxalica/rust-overlay](https://github.com/oxalica/rust-overlay) to
let you choose a specific channel of Rust. `rust-analyzer` is available in the
shell. It also contains a formatter configuration using
[treefmt-nix](https://github.com/numtide/treefmt-nix), which runs `rustfmt`.
It depends on:

It also contains a comment for quickly adding the default package that builds
the Rust package using [crane](https://github.com/ipetkov/crane). See [NixOS
Wiki](https://nixos.wiki/wiki/Rust#Packaging_Rust_projects_with_nix) for other
options.
- [oxalica/rust-overlay](https://github.com/oxalica/rust-overlay).
- [crane](https://github.com/ipetkov/crane).
- flake-parts.
- [treefmt-nix](https://github.com/numtide/treefmt-nix)

It provides:

- `rust-analyzer` for a selected Rust toolchain.
- Auto-formatting of Rust and Nix code via `nix fmt`.

### [elixir](elixir/)

Expand Down
136 changes: 71 additions & 65 deletions rust/flake.nix
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
{
inputs = {
# nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default";

# crane = {
# url = "github:ipetkov/crane";
# inputs.nixpkgs.follows = "nixpkgs";
# };
treefmt-nix.url = "github:numtide/treefmt-nix";

systems.url = "github:nix-systems/default";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay.url = "github:oxalica/rust-overlay";
crane.url = "github:ipetkov/crane";
};

outputs =
# Add settings for your binary cache.
nixConfig =
{
self,
systems,
nixpkgs,
treefmt-nix,
...
}@inputs:
let
eachSystem =
f:
nixpkgs.lib.genAttrs (import systems) (
system:
f (
import nixpkgs {
};

outputs =
inputs@{ nixpkgs, flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;

imports = [
inputs.treefmt-nix.flakeModule
];

perSystem =
{
config,
system,
pkgs,
lib,
craneLib,
commonArgs,
...
}:
{
_module.args = {
pkgs = import nixpkgs {
inherit system;
overlays = [ inputs.rust-overlay.overlays.default ];
}
)
);
};
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain (pkgs: pkgs.rust-bin.stable.latest.default);
commonArgs = {
# Depending on your code base, you may have to customize the
# source filtering to include non-standard files during the build.
# See
# https://crane.dev/source-filtering.html?highlight=source#source-filtering
src = craneLib.cleanCargoSource (craneLib.path ./.);

rustToolchain = eachSystem (pkgs: pkgs.rust-bin.stable.latest);
# nativeBuildInputs = with pkgs; [
# pkg-config
# ];

treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs ./treefmt.nix);
in
{
# You can use crane to build the Rust application with Nix
# buildInputs = with pkgs; [
# openssl
# ];
};
};

# packages = eachSystem (pkgs: let
# craneLib = inputs.crane.lib.${pkgs.system};
# in {
# default = craneLib.buildPackage {
# src = craneLib.cleanCargoSource (craneLib.path ./.);
# };
# });
# Build the executable package.
packages.default = craneLib.buildPackage (
commonArgs
// {
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
}
);

devShells = eachSystem (pkgs: {
# Based on a discussion at https://github.com/oxalica/rust-overlay/issues/129
default = pkgs.mkShell (
with pkgs;
{
nativeBuildInputs = [
clang
# Use mold when we are runnning in Linux
(lib.optionals stdenv.isLinux mold)
];
buildInputs = [
rustToolchain.${pkgs.system}.default
rust-analyzer-unwrapped
cargo
# pkg-config
# openssl
];
RUST_SRC_PATH = "${rustToolchain.${pkgs.system}.rust-src}/lib/rustlib/src/rust/library";
}
);
});
devShells.default = craneLib.devShell {
packages =
(commonArgs.nativeBuildInputs or [ ])
++ (commonArgs.buildInputs or [ ])
++ [ pkgs.rust-analyzer-unwrapped ];

formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper);
RUST_SRC_PATH = "${pkgs.rust-bin.stable.latest.rust-src}/lib/rustlib/src/rust/library";
};

checks = eachSystem (pkgs: {
formatting = treefmtEval.${pkgs.system}.config.build.check self;
});
treefmt = {
projectRootFile = "Cargo.toml";
programs = {
actionlint.enable = true;
nixfmt.enable = true;
rustfmt.enable = true;
};
};
};
};
}
6 changes: 0 additions & 6 deletions rust/treefmt.nix

This file was deleted.

0 comments on commit 96d8779

Please sign in to comment.