-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathflake.nix
More file actions
153 lines (131 loc) · 4.55 KB
/
flake.nix
File metadata and controls
153 lines (131 loc) · 4.55 KB
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
146
147
148
149
150
151
152
153
{
description = "Rust devShell with Fenix stable toolchain";
# Nix configuration: allow binary cache from nix-community to speed up builds
nixConfig = {
extra-substituters = [ "https://nix-community.cachix.org" ];
extra-trusted-public-keys = [
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs"
];
};
inputs = {
# Nixpkgs (unstable) for access to up-to-date packages
nixpkgs.url = "nixpkgs/nixos-unstable";
# Flake utils: helps iterate over multiple systems easily (e.g., x86_64-linux, aarch64-linux, macos)
flake-utils.url = "github:numtide/flake-utils";
# Rust-specific tools for analysis lifetime and ownership
rustowl-flake.url = "github:nix-community/rustowl-flake";
# Pre-commit hooks for Nix projects
git-hooks.url = "github:cachix/git-hooks.nix";
# Fenix: Rust toolchain via Nix (stable, nightly, minimal)
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
inputs@{
self,
nixpkgs,
flake-utils,
fenix,
rustowl-flake,
git-hooks,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
# Use LLVM 21 for clang/lld integration with Rust builds
llvm = pkgs.llvmPackages_21;
# Select the stable Rust toolchain from Fenix with required components
fenixPkgs = fenix.packages.${system}.stable;
rustToolchain = fenixPkgs.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
"rust-analyzer"
];
# Define git pre-commit hooks configuration
pre-commit-check = git-hooks.lib.${system}.run {
src = ./.;
hooks = {
# Enable Nix formatting check (nixfmt)
nixfmt-rfc-style.enable = true;
# Rust formatting (disabled by default)
rustfmt.enable = true;
# Clippy linting (enable by default)
clippy = {
enable = true;
packageOverrides = {
cargo = fenixPkgs.cargo;
clippy = fenixPkgs.clippy;
};
settings = {
allFeatures = true;
denyWarnings = true;
extraArgs = "--all-targets";
};
};
};
};
# Common tools needed for building and development
commonBuildInputs = with pkgs; [
rustowl-flake.packages.${system}.rustowl
rustToolchain
llvm.clang
llvm.lldb # debug with lldb-dap in VSCode
llvm.lld
llvm.libllvm
cargo-llvm-cov
openssl
sqlx-cli
sqlite
oha
nushell
postgresql
hey
just
k6
];
# Convert ymal to JSON for environment preparation
baseConfig = pkgs.runCommand "yaml-to-json" { } ''
${pkgs.yq-go}/bin/yq -o=json '.' ${./configuration/base.yml} > $out
'';
finalConfig = builtins.fromJSON (builtins.readFile baseConfig);
# Rust ≥1.90 uses lld by default. On NixOS this can fail due to missing system linker.
# We set clang + lld explicitly to avoid "linker not found" errors.
commonRustEnv = {
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = "${llvm.clang}/bin/clang";
RUSTFLAGS = "-Clink-arg=-fuse-ld=lld";
LIBCLANG_PATH = "${llvm.libclang.lib}/lib";
};
in
{
# Use nixfmt-rfc-style as default formatter for `nix fmt`
formatter = pkgs.nixfmt-rfc-style;
devShells = {
default = pkgs.mkShell (
commonRustEnv
// {
buildInputs = commonBuildInputs;
nativeBuildInputs = [ pkgs.pkg-config ];
shellHook = ''
# Export key variables from the parsed config
# export API_KEY=${finalConfig.application.api_key}
# Ensure OpenSSL libs are available at runtime
export LD_LIBRARY_PATH="${pkgs.lib.makeLibraryPath [ pkgs.openssl ]}:$LD_LIBRARY_PATH"
# enable pre-commit hook installation inside the dev shell
${pre-commit-check.shellHook}
echo "[info] Using Fenix (stable) Rust toolchain."
'';
}
);
};
# Optional: expose pre-commit checks to CI (e.g. `nix flake check`)
# checks = { inherit pre-commit-check; };
}
);
}