Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
221 lines (200 loc) · 7.98 KB
/
Copy pathflake.nix
File metadata and controls
221 lines (200 loc) · 7.98 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
{
description = "Xcind Development Environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
let
xcindVersion = "0.5.0";
# Convert self.lastModifiedDate (compact "YYYYMMDDhhmmss") to ISO 8601.
# Returns empty string when unavailable so xcind-build-info degrades
# gracefully in exotic evaluation contexts.
formatFlakeDate = d:
if d == null || d == "" then ""
else
let
yyyy = builtins.substring 0 4 d;
mm = builtins.substring 4 2 d;
dd = builtins.substring 6 2 d;
hh = builtins.substring 8 2 d;
mi = builtins.substring 10 2 d;
ss = builtins.substring 12 2 d;
in "${yyyy}-${mm}-${dd}T${hh}:${mi}:${ss}Z";
# Build-provenance attrs derived from the flake's own `self`. Passed as
# a plain attribute set to each derivation so postInstall can emit
# xcind-build-info.bash without re-evaluating flake metadata.
buildInfo = {
shortRev = self.shortRev or self.dirtyShortRev or "";
longRev = self.rev or self.dirtyRev or "";
date = formatFlakeDate (self.lastModifiedDate or "");
dirty = if self ? dirtyRev then "1" else "0";
};
# postInstall snippet that writes xcind-build-info.bash. Shared between
# wrapped and unwrapped derivations so both channels report provenance.
writeBuildInfo = ''
cat > "$out/lib/xcind/xcind-build-info.bash" <<'BUILDINFO_EOF'
#!/usr/bin/env bash
# xcind-build-info.bash — Generated by Nix flake. Do not edit.
XCIND_BUILD_SOURCE="nix"
XCIND_BUILD_SHORT_REV="${buildInfo.shortRev}"
XCIND_BUILD_LONG_REV="${buildInfo.longRev}"
XCIND_BUILD_REF=""
XCIND_BUILD_DATE="${buildInfo.date}"
XCIND_BUILD_DIRTY="${buildInfo.dirty}"
BUILDINFO_EOF
chmod 644 "$out/lib/xcind/xcind-build-info.bash"
'';
# Runtime dependency sets, factored so the wrapped build and anything
# else that needs the same PATH stays in sync.
#
# yq is required — the default-registered hooks (proxy, workspace,
# app-env, app, host-gateway) all need it. We use yq-go (mikefarah) for
# a single static binary with no jq propagation. Users bringing their
# own yq on PATH (e.g. via the unwrapped package below) can also use
# kislyuk/yq (the Python jq wrapper) — Xcind's yq usage is compatible
# with both.
#
# jq is only needed by the JSON-emitting binaries (xcind-config --json,
# xcind-workspace --json) and for the config.json cache side-effect
# produced by xcind-compose.
#
# docker is only needed by binaries that shell out to `docker compose`.
xcindCoreDeps = pkgs: [ pkgs.coreutils pkgs.yq-go ];
xcindJsonDeps = pkgs: [ pkgs.jq ];
xcindDockerDeps = pkgs: [ pkgs.docker ];
# Base attrs shared by both the wrapped and unwrapped derivations.
baseAttrs = {
version = xcindVersion;
src = ./.;
dontBuild = true;
installPhase = ''
runHook preInstall
bash ./install.sh "$out"
runHook postInstall
'';
};
# Unwrapped build: installs xcind to $out but does not inject any
# runtime dependencies. Users must provide jq/yq/docker themselves via
# their own environment. Useful when downstream consumers want to pin
# different tool versions or have jq/yq already on PATH.
mkXcindMinimal = pkgs: pkgs.stdenv.mkDerivation (baseAttrs // {
pname = "xcind-minimal";
postInstall = writeBuildInfo;
meta = with pkgs.lib; {
description = "Docker Compose environment manager (unwrapped — bring your own jq/yq/docker)";
license = licenses.mit;
platforms = platforms.unix;
mainProgram = "xcind";
};
});
# Wrapped build: same install, but each xcind-* binary is wrapped with
# a PATH that contains its runtime dependencies so the binaries work
# regardless of what the user has installed.
mkXcind = pkgs: pkgs.stdenv.mkDerivation (baseAttrs // {
pname = "xcind";
nativeBuildInputs = [ pkgs.makeWrapper ];
postInstall =
let
core = xcindCoreDeps pkgs;
json = xcindJsonDeps pkgs;
docker = xcindDockerDeps pkgs;
# xcind-compose, xcind-config, and xcind-workspace all need the
# full set at runtime: they either exec `docker compose` directly
# or go through __xcind-populate-cache, which shells out to
# `docker compose config`.
fullPath = pkgs.lib.makeBinPath (core ++ json ++ docker);
# xcind-proxy manages its own Traefik stack via `docker compose
# -f <fixed file>` and never runs the xcind pipeline. It still
# needs jq for the --json variant of `status`, which serialises
# the assigned-ports state as a JSON array.
proxyPath = pkgs.lib.makeBinPath (core ++ json ++ docker);
in writeBuildInfo + ''
wrapProgram "$out/bin/xcind" \
--prefix PATH : ${fullPath}
wrapProgram "$out/bin/xcind-application" \
--prefix PATH : ${fullPath}
wrapProgram "$out/bin/xcind-compose" \
--prefix PATH : ${fullPath}
wrapProgram "$out/bin/xcind-config" \
--prefix PATH : ${fullPath}
wrapProgram "$out/bin/xcind-prompt" \
--prefix PATH : ${fullPath}
wrapProgram "$out/bin/xcind-proxy" \
--prefix PATH : ${proxyPath}
wrapProgram "$out/bin/xcind-run" \
--prefix PATH : ${fullPath}
wrapProgram "$out/bin/xcind-workspace" \
--prefix PATH : ${fullPath}
'';
meta = with pkgs.lib; {
description = "Docker Compose environment manager";
license = licenses.mit;
platforms = platforms.unix;
mainProgram = "xcind";
};
});
in
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
# Python environment for building Sphinx docs (docs/source/)
pythonEnv = pkgs.python3.withPackages (ps: with ps; [
sphinx
sphinx-rtd-theme
myst-parser
]);
in
{
packages = {
default = mkXcind pkgs;
xcind = mkXcind pkgs;
xcind-minimal = mkXcindMinimal pkgs;
};
devShells.default = pkgs.mkShell {
name = "xcind-dev";
buildInputs = with pkgs; [
# -- Core runtime & testing --
bash
coreutils
ncurses
parallel
# -- Code quality (tools used by .pre-commit-config.yaml) --
shellcheck
shfmt
gitleaks
pre-commit
# -- npm packaging --
nodejs
# -- Documentation (Sphinx) --
pythonEnv
# -- Container builds --
docker
docker-compose
# -- Utilities --
jq
yq-go
curl
git
git-cliff
];
shellHook = ''
# Let .pre-commit-config.yaml be the single source of truth
if [ -f .pre-commit-config.yaml ] && [ -d .git ]; then
pre-commit install --allow-missing-config > /dev/null 2>&1 || true
fi
export PATH="$PWD/bin:$PATH"
echo "xcind dev shell ready"
echo " node : $(node --version)"
echo " shellcheck: $(shellcheck --version | head -2 | tail -1)"
echo " sphinx : $(sphinx-build --version)"
'';
};
}
) // {
overlays.default = final: _prev: {
xcind = mkXcind final;
xcind-minimal = mkXcindMinimal final;
};
};
}