Skip to content

Commit

Permalink
feat: Allow importing of crate.nix automatically
Browse files Browse the repository at this point in the history
By default, nothing happens - unless the user sets the `crateNixFile` option.
  • Loading branch information
srid committed Aug 5, 2024
1 parent ca8228d commit 747daa6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 12 additions & 0 deletions nix/modules/default-crates.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@
lib.cleanSourceWith {
name = builtins.baseNameOf pathString;
src = "${src}/${pathString}";
# TODO(DRY): Consolidate with that of flake-module.nix
filter = path: type:
(config.rust-project.crateNixFile != null && lib.hasSuffix "/${config.rust-project.crateNixFile}" path) ||
(config.rust-project.crane-lib.filterCargoSources path type);
};
cargoPath = "${path}/Cargo.toml";
cargoToml = builtins.fromTOML (builtins.readFile cargoPath);
name = cargoToml.package.name;
crateNixFilePath =
if config.rust-project.crateNixFile == null
then null
else
let p = "${path}/${config.rust-project.crateNixFile}"; in
if lib.pathIsRegularFile p then p else null;
in
acc // {
${name} = {
# Import the .nix file from the crate directory, if asked for.
imports = lib.optionals (crateNixFilePath != null) [
(builtins.traceVerbose "rust-flake: Using ${crateNixFilePath}" crateNixFilePath)
];
path = lib.mkDefault path;
};
}
Expand Down
18 changes: 16 additions & 2 deletions nix/modules/flake-module.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rustFlakeInputs:
{ self, pkgs, lib, flake-parts-lib, ... }:
{ inputs, self, pkgs, lib, flake-parts-lib, ... }:

let
inherit (flake-parts-lib)
Expand All @@ -21,8 +21,9 @@ in
type = lib.types.attrsOf (lib.types.submoduleWith {
modules = [ ./crate.nix ];
specialArgs = {
flake = { inherit inputs; };
inherit (config) rust-project;
inherit pkgs;
inherit pkgs system;
};
});
};
Expand All @@ -43,12 +44,25 @@ in
};
};

crateNixFile = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = ''
The Nix file to import automatically if it exists in the
crate directory.
By default, nothing is automagically imported.
'';
default = null;
};

src = lib.mkOption {
type = lib.types.path;
description = "Source directory for the rust-project package";
default = lib.cleanSourceWith {
src = self; # The original, unfiltered source
# TODO(DRY): Consolidate with that of default-crates.nix
filter = path: type:
(config.rust-project.crateNixFile != null && lib.hasSuffix "/${config.rust-project.crateNixFile}" path) ||
# Default filter from crane (allow .rs files)
(config.rust-project.crane-lib.filterCargoSources path type)
;
Expand Down

0 comments on commit 747daa6

Please sign in to comment.