Skip to content

Commit

Permalink
tools.nix: Also expose as perSystem attribute "tools" in flake
Browse files Browse the repository at this point in the history
  • Loading branch information
kolloch committed Jan 7, 2024
1 parent da53907 commit b1552fd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
35 changes: 32 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,50 @@
imports = [
./nix/devshell/flake-module.nix
./nix/pre-commit/flake-module.nix
./nix/perSystem-tools/flake-module.nix
./crate2nix/flake-module.nix
./docs/flake-module.nix
];

flake = {
templates.default = {
flake = { lib, ... }: {
config.templates.default = {
path = ./template;
description = "An example of crate2nix";
};

config.lib = {
tools = import ./tools.nix;
};

options.lib = lib.mkOption {
description = ''
nix libraries exported by crate2nix.
'';

type = lib.types.submoduleWith {
modules = [
{
options.tools = lib.mkOption {
description = ''
Prefer the perSystem "tools" option which has the libary
already applied for the correct system.
Export the crate2nix/tools.nix function as property.
To use it, call it with pkgs.callPackage.
'';

type = lib.types.functionTo lib.types.attrs;
};
}
];
};
};
};

perSystem = { config, self', inputs', pkgs, system, ... }: {
formatter = pkgs.nixpkgs-fmt;
checks = config.packages;
packages.niv = pkgs.niv;
};
};
}
62 changes: 62 additions & 0 deletions nix/perSystem-tools/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
# provider perSystem
transposition.tools = { };

perSystem = { config, pkgs, lib, ... }: {
options.tools = lib.mkOption {
description = ''
Library functions to generate the `Cargo.nix` build
file automatically.
'';

type = lib.types.submoduleWith {
modules = [
{
options.generatedCargoNix = lib.mkOption {
description = ''
Returns a derivation containing the generated `Cargo.nix` file
which can be called with `pkgs.callPackage`.
name: will be part of the derivation name
src: the source that is needed to build the crate, usually the
crate/workspace root directory
cargoToml: Path to the Cargo.toml file relative to src, "Cargo.toml" by
default.
'';
type = lib.types.functionTo lib.types.package;
};
options.appliedCargoNix = lib.mkOption {
description = ''
Applies the default arguments from pkgs to the generated `Cargo.nix` file.
name: will be part of the derivation name
src: the source that is needed to build the crate, usually the crate/workspace root directory
cargoToml: Path to the Cargo.toml file relative to src, "Cargo.toml" by default.
'';
type = lib.types.functionTo lib.types.attrs;
};
}
];
};
};

config.tools =
let tools = pkgs.callPackage ../../tools.nix { };
in {
inherit (tools) generatedCargoNix appliedCargoNix;
};

config.checks = {
toolsGeneratedCargoNix_crate2nix =
let
cargoNixBuilder = config.tools.generatedCargoNix { name = "crate2nix"; src = ../../crate2nix; };
cargoNix = pkgs.callPackage cargoNixBuilder { };
in
cargoNix.rootCrate.build;

toolsAppliedCargoNix_crate2nix =
let cargoNix = config.tools.appliedCargoNix { name = "crate2nix"; src = ../../crate2nix; };
in cargoNix.rootCrate.build;
};
};
}
4 changes: 2 additions & 2 deletions tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let
in
rec {

/* Returns the whole top-level function generated by crate2nix (`Cargo.nix`)
which is typically called with `pkgs.callPackage`.
/* Returns a derivation containing the whole top-level function generated
by crate2nix (`Cargo.nix`) which is typically called with `pkgs.callPackage`.
name: will be part of the derivation name
src: the source that is needed to build the crate, usually the
Expand Down

0 comments on commit b1552fd

Please sign in to comment.