generated from srid/rust-nix-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflake-module.nix
145 lines (134 loc) · 5.15 KB
/
flake-module.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{ self, lib, inputs, flake-parts-lib, ... }:
let
inherit (flake-parts-lib)
mkPerSystemOption;
in
{
options = {
perSystem = mkPerSystemOption
({ config, self', inputs', pkgs, system, ... }: {
options = {
leptos-fullstack.overrideCraneArgs = lib.mkOption {
type = lib.types.functionTo lib.types.attrs;
default = _: { };
description = "Override crane args for the leptos-fullstack package";
};
leptos-fullstack.rustToolchain = lib.mkOption {
type = lib.types.package;
description = "Rust toolchain to use for the leptos-fullstack package";
default = (pkgs.rust-bin.fromRustupToolchainFile (self + /rust-toolchain.toml)).override {
extensions = [
"rust-src"
"rust-analyzer"
"clippy"
];
};
};
leptos-fullstack.craneLib = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
default = (inputs.crane.mkLib pkgs).overrideToolchain config.leptos-fullstack.rustToolchain;
};
leptos-fullstack.src = lib.mkOption {
type = lib.types.path;
description = "Source directory for the leptos-fullstack package";
# When filtering sources, we want to allow assets other than .rs files
# TODO: Don't hardcode these!
default = lib.cleanSourceWith {
src = self; # The original, unfiltered source
filter = path: type:
(lib.hasSuffix "\.html" path) ||
(lib.hasSuffix "tailwind.config.js" path) ||
# Example of a folder for images, icons, etc
(lib.hasInfix "/assets/" path) ||
(lib.hasInfix "/css/" path) ||
# Default filter from crane (allow .rs files)
(config.leptos-fullstack.craneLib.filterCargoSources path type)
;
};
};
};
config =
let
cargoToml = builtins.fromTOML (builtins.readFile (self + /Cargo.toml));
inherit (cargoToml.package) name version;
inherit (config.leptos-fullstack) rustToolchain craneLib src;
# Crane builder for cargo-leptos projects
craneBuild = rec {
args = {
inherit src;
pname = name;
version = version;
buildInputs = [
pkgs.cargo-leptos
pkgs.binaryen # Provides wasm-opt
tailwindcss
];
};
cargoArtifacts = craneLib.buildDepsOnly args;
buildArgs = args // {
inherit cargoArtifacts;
buildPhaseCargoCommand = "cargo leptos build --release -vvv";
cargoTestCommand = "cargo leptos test --release -vvv";
nativeBuildInputs = [
pkgs.makeWrapper
];
installPhaseCommand = ''
mkdir -p $out/bin
cp target/server/release/${name} $out/bin/
cp -r target/site $out/bin/
wrapProgram $out/bin/${name} \
--set LEPTOS_SITE_ROOT $out/bin/site
'';
};
package = craneLib.buildPackage (buildArgs // config.leptos-fullstack.overrideCraneArgs buildArgs);
check = craneLib.cargoClippy (args // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --all-features -- --deny warnings";
});
doc = craneLib.cargoDoc (args // {
inherit cargoArtifacts;
});
};
rustDevShell = pkgs.mkShell {
shellHook = ''
# For rust-analyzer 'hover' tooltips to work.
export RUST_SRC_PATH="${rustToolchain}/lib/rustlib/src/rust/library";
'';
buildInputs = [
pkgs.libiconv
];
nativeBuildInputs = [
rustToolchain
];
};
tailwindcss = pkgs.nodePackages.tailwindcss.overrideAttrs
(oa: {
plugins = [
pkgs.nodePackages."@tailwindcss/aspect-ratio"
pkgs.nodePackages."@tailwindcss/forms"
pkgs.nodePackages."@tailwindcss/language-server"
pkgs.nodePackages."@tailwindcss/line-clamp"
pkgs.nodePackages."@tailwindcss/typography"
];
});
in
{
# 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
];
nativeBuildInputs = with pkgs; [
tailwindcss
cargo-leptos
binaryen # Provides wasm-opt
];
};
};
});
};
}