-
Notifications
You must be signed in to change notification settings - Fork 1
/
flakelight-rust.nix
72 lines (61 loc) · 2.15 KB
/
flakelight-rust.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# flakelight-rust -- Rust module for flakelight
# Copyright (C) 2023 Archit Gupta <[email protected]>
# SPDX-License-Identifier: MIT
{ lib, src, config, flakelight, ... }:
let
inherit (builtins) elem readFile pathExists;
inherit (lib) mkDefault mkIf mkMerge mkOption warnIf;
inherit (lib.fileset) fileFilter toSource;
inherit (flakelight.types) fileset;
cargoToml = fromTOML (readFile (src + /Cargo.toml));
tomlPackage = cargoToml.package or cargoToml.workspace.package;
in
warnIf (! builtins ? readFileType) "Unsupported Nix version in use."
{
options.fileset = mkOption {
type = fileset;
default = fileFilter
(file: file.hasExt "rs" || elem file.name [ "Cargo.toml" "Cargo.lock" ])
src;
};
config = mkMerge [
(mkIf (pathExists (src + /Cargo.toml)) {
withOverlays = final: { inputs, ... }: rec {
craneLib = inputs.crane.mkLib final;
cargoArtifacts = craneLib.buildDepsOnly
{ inherit src; strictDeps = true; };
};
description = mkIf (tomlPackage ? description) tomlPackage.description;
# license will need to be set if Cargo license is a complex expression
license = mkIf (tomlPackage ? license) (mkDefault tomlPackage.license);
pname = tomlPackage.name;
package = { craneLib, cargoArtifacts, defaultMeta }:
craneLib.buildPackage {
src = toSource { root = src; inherit (config) fileset; };
inherit cargoArtifacts;
doCheck = false;
strictDeps = true;
meta = defaultMeta;
};
checks = { craneLib, cargoArtifacts, ... }: {
test = craneLib.cargoTest { inherit src cargoArtifacts; };
clippy = craneLib.cargoClippy {
inherit src cargoArtifacts;
strictDeps = true;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
};
};
})
{
devShell = {
packages = pkgs: with pkgs; [ rust-analyzer cargo clippy rustc rustfmt ];
env = { rustPlatform, ... }: {
RUST_SRC_PATH = "${rustPlatform.rustLibSrc}";
};
};
formatters = pkgs: {
"*.rs" = "${pkgs.rustfmt}/bin/rustfmt";
};
}
];
}