-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnix-common.nix
More file actions
150 lines (135 loc) · 4.35 KB
/
nix-common.nix
File metadata and controls
150 lines (135 loc) · 4.35 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
# Shared Nix configuration for shell.nix and flake.nix
#
# This file ensures both environments have identical packages, environment
# variables, and shell hooks. The flake.lock provides exact version pinning.
#
# Usage in flake.nix:
# nixCommon = import ./nix-common.nix { inherit pkgs; };
#
# Usage in shell.nix:
# nixCommon = import ./nix-common.nix { inherit pkgs; };
{ pkgs }:
let
# Rust toolchain from rust-toolchain.toml (includes rustc, cargo, rustfmt, clippy)
# Works because both shell.nix and flake.nix apply rust-overlay before importing
# Uses pkgs/id path directly (root symlink not tracked by git, won't be in Nix store)
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./pkgs/id/rust-toolchain.toml;
# Formatter binaries (keep in sync with treefmt.toml)
# Used by formatter wrapper (nix fmt), nix flake checks, and devShell PATH
fmtBins = [
# Rust toolchain (includes rustfmt)
rustToolchain
]
++ (with pkgs; [
# Formatter orchestrator
treefmt
# Formatters and linters (keep in sync with treefmt.toml)
nixfmt
statix
biome
nodePackages.prettier
shfmt
shellcheck
ruff
rufo
elmPackages.elm-format
go
haskellPackages.ormolu
taplo
# Utilities needed by formatter wrapper
file
just
gnused
findutils
bash
]);
# Native build inputs (tools) — used by nativeBuildInputs and packages
nativeBuildInputs =
fmtBins
++ (with pkgs; [
# Build dependencies (Rust compilation)
pkg-config
openssl
nix
home-manager
git
sops
ssh-to-age
gnupg
age
# Python/uv (used by scripts)
uv
# Manual linters (not in treefmt, run manually)
deadnix
]);
in
{
inherit rustToolchain fmtBins nativeBuildInputs;
NIX_CONFIG = "extra-experimental-features = nix-command flakes ca-derivations";
# Anchor bare treefmt to current directory (for ad-hoc devshell use)
# (just recipes and nix fmt use explicit --config-file/--tree-root instead)
TREEFMT_TREE_ROOT_CMD = "pwd";
# Build inputs (libraries for Rust compilation)
buildInputs = with pkgs; [ openssl ];
# OpenSSL environment variables
opensslEnv = {
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include";
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
};
# Packages for shell.nix / nix develop (nativeBuildInputs + extra runtime deps)
packages = nativeBuildInputs ++ [
(pkgs.python3.withPackages (
python-pkgs: with python-pkgs; [
pydbus
dbus-python
pygobject3
dbus-python
]
))
pkgs.gobject-introspection
pkgs.glib
];
# Shared shell hook for both nix-shell and nix develop
shellHook = ''
# nix develop reconstructs PATH, losing HM session paths.
# Restore them: devshell → HM → system
# Guard: only do PATH reordering once (multiple use flake in .envrc)
if [ -z "$__DEVSHELL_HM_PATH_DONE" ]; then
export __DEVSHELL_HM_PATH_DONE=1
# Source HM session vars and extract any PATH additions
_pre_profile_path="$PATH"
unset __HM_SESS_VARS_SOURCED
. "$HOME/.profile" 2>/dev/null || true
_hm_paths="''${PATH%:$_pre_profile_path}"
[ "$_hm_paths" = "$PATH" ] && _hm_paths=""
# Separate devshell from system using NIX_PROFILES (NixOS env var)
# Non-NixOS: NIX_PROFILES is unset, everything stays in devshell, PATH unchanged
_devshell_only=""
_system_only=""
_remaining="$_pre_profile_path"
while [ -n "$_remaining" ]; do
_e="''${_remaining%%:*}"
[ "$_e" = "$_remaining" ] && _remaining="" || _remaining="''${_remaining#*:}"
_is_sys=0
for _p in $NIX_PROFILES; do
case "$_e" in "$_p"/*) _is_sys=1; break ;; esac
done
if [ "$_is_sys" = 1 ]; then
_system_only="$_system_only''${_system_only:+:}$_e"
else
_devshell_only="$_devshell_only''${_devshell_only:+:}$_e"
fi
done
# Reconstruct: devshell → HM → system
export PATH="$_devshell_only''${_hm_paths:+:$_hm_paths}''${_system_only:+:$_system_only}"
unset _pre_profile_path _hm_paths _devshell_only _system_only _remaining _e _is_sys _p
fi
echo ""
just --list
echo ""
echo "Welcome to the development shell!"
echo ""
'';
}