-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
133 lines (118 loc) · 4.06 KB
/
flake.nix
File metadata and controls
133 lines (118 loc) · 4.06 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
{
description = "A Nix-flake-based Rust development environment with pre-commit shell hook";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
git-hooks-nix.inputs.nixpkgs.follows = "nixpkgs";
git-hooks-nix.url = "github:cachix/git-hooks.nix";
flake-parts.url = "github:hercules-ci/flake-parts";
nix2container.url = "github:nlewo/nix2container";
};
outputs = inputs @ {
self,
flake-parts,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [inputs.git-hooks-nix.flakeModule];
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
perSystem = {
pkgs,
config,
system,
...
}: let
/*
Toolchain selection:
- This uses nixpkgs' Rust (rustc/cargo) so it's reproducible across machines.
- If you want a specific channel/version, you can override nixpkgs or
swap to fenix/rustup in the future.
*/
rustc = pkgs.rustc;
cargo = pkgs.cargo;
rustfmt = pkgs.rustfmt;
clippy = pkgs.clippy;
analyzer = pkgs.rust-analyzer;
# Helpful extras—adjust to taste
cargoComponents = with pkgs; [
cargo-edit
cargo-watch
cargo-nextest
cargo-audit
pkg-config
];
# Displayed in the shell greeting
rustVersion = rustc.version;
nix2containerPkgs = inputs.nix2container.packages.${system};
rustHello = pkgs.rustPlatform.buildRustPackage {
pname = "rust-hello";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
doCheck = false;
postInstall = ''
strip --strip-unneeded $out/bin/*
'';
};
in {
packages = {
default = rustHello;
rust-hello = rustHello;
container = nix2containerPkgs.nix2container.buildImage {
name = "docker.io/allheil/rust-hello";
tag = "latest";
copyToRoot = [
rustHello
];
config = {
Cmd = ["/bin/rust_hello"];
ExposedPorts."3000/tcp" = {};
};
};
};
# Pre-commit hooks configuration (mirrors your Python setup’s style)
pre-commit.settings = {
# Anything listed as a hook will be made available to the devShell
# via `enabledPackages`, which we include in packages below.
hooks = {
# Keep Nix files tidy
alejandra.enable = true;
# Rust format/lints
rustfmt.enable = true;
# clippy.enable = true;
# Optional: run a fast build check (uncomment if desired)
# cargo-check.enable = true;
# Optional: security audit (can be slower on large workspaces)
# cargo-audit.enable = true;
};
};
# Dev shell with shellHook installing pre-commit
devShells.default = pkgs.mkShell {
shellHook = ''
${config.pre-commit.installationScript}
echo 1>&2 "Welcome to the development shell (Rust ${rustVersion})!"
echo 1>&2 " - rustc: $(${pkgs.coreutils}/bin/printf '%s' "$(${rustc}/bin/rustc --version)")"
echo 1>&2 " - cargo: $(${pkgs.coreutils}/bin/printf '%s' "$(${cargo}/bin/cargo --version)")"
# echo 1>&2 " - clippy: $(${pkgs.coreutils}/bin/printf '%s' "$(${clippy}/bin/cargo-clippy --version 2>/dev/null || echo 'available via cargo clippy')")"
echo 1>&2 " - rustfmt: $(${pkgs.coreutils}/bin/printf '%s' "$(${rustfmt}/bin/rustfmt --version)")"
'';
# Tools needed for your workflow, plus anything required by hooks.
packages =
config.pre-commit.settings.enabledPackages
++ [
rustc
cargo
rustfmt
clippy
analyzer
]
++ cargoComponents;
};
};
flake = {};
};
}