Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial version of the flake module #1

Merged
merged 10 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# rust-flake

WIP: Like the famous `haskell-flake`, but for Rust.

## Progress

- [x] Simple module that works with single-create projects
- [ ] Multi-crate workspaces
- [ ] Multiple projects
- [ ] Examples & tests
118 changes: 118 additions & 0 deletions flake.lock

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

16 changes: 16 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
description = "A `flake-parts` module for Rust development";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
# https://github.com/ipetkov/crane/issues/527
crane.url = "github:ipetkov/crane/2c653e4478476a52c6aa3ac0495e4dea7449ea0e"; # Cargo.toml parsing is broken in newer crane (Mar 24)
crane.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { rust-overlay, crane, ... }: {
flakeModules = {
default = import ./nix/modules/flake-module.nix { inherit rust-overlay crane; };
nixpkgs = import ./nix/modules/nixpkgs.nix;
};
};
}
125 changes: 125 additions & 0 deletions nix/modules/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
inputs:
{ self, lib, flake-parts-lib, ... }:

let
inherit (flake-parts-lib)
mkPerSystemOption;
in
{
options = {
perSystem = mkPerSystemOption
({ config, self', pkgs, system, ... }: {
options = {
# TODO: Multiple projects
# TODO: Workspace crates
rust-project.crane.args.buildInputs = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [ ];
description = "(Runtime) buildInputs for the cargo package";
};
rust-project.crane.args.nativeBuildInputs = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = with pkgs; [
pkg-config
makeWrapper
];
description = "nativeBuildInputs for the cargo package";
};
rust-project.crane.lib = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
default = (inputs.crane.mkLib pkgs).overrideToolchain config.rust-project.toolchain;
};


rust-project.toolchain = lib.mkOption {
type = lib.types.package;
description = "Rust toolchain to use for the rust-project package";
default = (pkgs.rust-bin.fromRustupToolchainFile (self + /rust-toolchain.toml)).override {
extensions = [
"rust-src"
"rust-analyzer"
"clippy"
];
};
};

rust-project.src = lib.mkOption {
type = lib.types.path;
description = "Source directory for the rust-project package";
default = lib.cleanSourceWith {
src = self; # The original, unfiltered source
filter = path: type:
# Default filter from crane (allow .rs files)
(config.rust-project.crane.lib.filterCargoSources path type)
;
};
};
};
config =
let
cargoToml = builtins.fromTOML (builtins.readFile (self + /Cargo.toml));
inherit (cargoToml.package) name version;
inherit (config.rust-project) toolchain crane src;

# Crane builder for Dioxus projects projects
craneBuild = rec {
args = {
inherit src;
inherit (crane.args) buildInputs nativeBuildInputs;
pname = name;
version = version;
# glib-sys fails to build on linux without this
# cf. https://github.com/ipetkov/crane/issues/411#issuecomment-1747533532
strictDeps = true;
};
cargoArtifacts = crane.lib.buildDepsOnly args;
buildArgs = args // {
inherit cargoArtifacts;
};
package = crane.lib.buildPackage buildArgs;

check = crane.lib.cargoClippy (args // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --all-features -- --deny warnings";
});

doc = crane.lib.cargoDoc (args // {
inherit cargoArtifacts;
});
};

rustDevShell = pkgs.mkShell {
shellHook = ''
# For rust-analyzer 'hover' tooltips to work.
export RUST_SRC_PATH="${toolchain}/lib/rustlib/src/rust/library";
'';
buildInputs = [
pkgs.libiconv
] ++ config.rust-project.crane.args.buildInputs;
packages = [
toolchain
];
};
in
{
# See nix/modules/nixpkgs.nix (the user must import it)
nixpkgs.overlays = [
inputs.rust-overlay.overlays.default
];

# Rust package
packages.${name} = craneBuild.package;
packages."${name}-doc" = craneBuild.doc;

checks."${name}-clippy" = craneBuild.check;

# Rust dev environment
devShells.${name} = pkgs.mkShell {
inputsFrom = [
rustDevShell
];
};
};
});
};
}
9 changes: 9 additions & 0 deletions nix/modules/nixpkgs.nix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://github.com/hercules-ci/flake-parts/issues/74#issuecomment-1513708722
{ inputs, flake-parts-lib, ... }: {
options.perSystem = flake-parts-lib.mkPerSystemOption ({ pkgs, system, ... }: {
imports = [
"${inputs.nixpkgs}/nixos/modules/misc/nixpkgs.nix"
];
nixpkgs.hostPlatform = system;
});
}