diff --git a/nix/modules/default-crates.nix b/nix/modules/default-crates.nix index 7cdcd68..897bde9 100644 --- a/nix/modules/default-crates.nix +++ b/nix/modules/default-crates.nix @@ -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; }; } diff --git a/nix/modules/flake-module.nix b/nix/modules/flake-module.nix index 6c10649..055544b 100644 --- a/nix/modules/flake-module.nix +++ b/nix/modules/flake-module.nix @@ -1,5 +1,5 @@ rustFlakeInputs: -{ self, pkgs, lib, flake-parts-lib, ... }: +{ inputs, self, pkgs, lib, flake-parts-lib, ... }: let inherit (flake-parts-lib) @@ -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; }; }); }; @@ -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) ;