cuenv 0.4.7

A direnv alternative that uses CUE files for environment configuration
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
{
  description = "cuenv - A direnv alternative that uses CUE files for environment configuration";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    fenix = {
      url = "github:nix-community/fenix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    flake-utils.url = "github:numtide/flake-utils";
    treefmt-nix = {
      url = "github:numtide/treefmt-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    { self
    , nixpkgs
    , rust-overlay
    , fenix
    , flake-utils
    , treefmt-nix
    ,
    }:
    (flake-utils.lib.eachDefaultSystem
      (
        system:
        let
          overlays = [ (import rust-overlay) ];
          pkgs = import nixpkgs {
            inherit system overlays;
          };

          # Platform-specific dependencies
          platformDeps =
            with pkgs;
            if stdenv.isDarwin then
              [
                darwin.apple_sdk.frameworks.Security
                darwin.apple_sdk.frameworks.CoreFoundation
              ]
            else if stdenv.isLinux then
              [
                glibc
              ]
            else
              [ ];

          # Build dependencies
          buildInputs =
            with pkgs;
            [
              go_1_24
              openssl
              pkg-config
            ]
            ++ platformDeps;

          # Native build dependencies
          nativeBuildInputs = with pkgs; [
            (rust-bin.stable."1.88.0".default.override {
              extensions = [ "rust-src" "llvm-tools-preview" ];
            })
            pkg-config
            gcc
            go_1_24
            protobuf
          ];

          # treefmt configuration
          treefmt = treefmt-nix.lib.evalModule pkgs {
            projectRootFile = "flake.nix";
            programs = {
              # Nix formatter
              nixpkgs-fmt.enable = true;

              cue.enable = true;

              # Rust formatter
              rustfmt = {
                enable = true;
                # Use same edition as Cargo.toml
                edition = "2021";
              };

              # Go formatter
              gofmt.enable = true;

              # YAML formatter
              yamlfmt.enable = true;

              # Prettier formatter - handles markdown with frontmatter support
              prettier = {
                enable = true;
                # Only format markdown files with prettier
                includes = [
                  "*.md"
                  "*.mdx"
                ];
              };

              # TOML formatter
              taplo.enable = true;
            };
          };

          # Development tools
          devTools = with pkgs; [
            cargo-watch
            cargo-edit
            cargo-outdated
            cargo-audit
            cargo-nextest
            cue
            gopls
            gotools
            rust-analyzer
            treefmt.config.build.wrapper
            protobuf
            grpcurl
            netcat
          ];

          # Read version from Cargo.toml
          cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
          version = cargoToml.package.version;

          # Vendor Go dependencies
          goVendor = pkgs.stdenv.mkDerivation {
            pname = "cuenv-go-vendor";
            version = version;
            src = ./libcue-bridge;

            nativeBuildInputs = [ pkgs.go_1_24 ];

            buildPhase = ''
              export HOME=$(mktemp -d)
              export GOPATH="$HOME/go"
              export GOCACHE="$HOME/go-cache"

              go mod vendor
            '';

            installPhase = ''
              mkdir -p $out
              cp -r vendor $out/
            '';

            outputHashMode = "recursive";
            outputHashAlgo = "sha256";
            outputHash = "sha256-l/urNOAK9q5nflBt2ovfbaM3WNCn0ouZlc9RJq/+eKk=";
          };

          cuenv = pkgs.rustPlatform.buildRustPackage {
            pname = "cuenv";
            version = version;

            src = ./.;

            cargoLock = {
              lockFile = ./Cargo.lock;
            };

            # Set up build environment
            preBuild = ''
              export HOME=$(mktemp -d)
              export GOPATH="$HOME/go"
              export GOCACHE="$HOME/go-cache"
              export CGO_ENABLED=1

              # Copy vendored dependencies
              cp -r ${goVendor}/vendor libcue-bridge/
              chmod -R u+w libcue-bridge
            '';

            inherit buildInputs nativeBuildInputs;

            # Platform-specific linker flags
            RUSTFLAGS =
              if pkgs.stdenv.isDarwin then
                "-C link-arg=-framework -C link-arg=Security -C link-arg=-framework -C link-arg=CoreFoundation"
              else
                "";

            # Ensure Go is available during build
            CGO_ENABLED = "1";
            GO = "${pkgs.go_1_24}/bin/go";

            # Disable tests during build phase - we'll run them separately
            doCheck = false;

            meta = with pkgs.lib; {
              description = "A direnv alternative that uses CUE files for environment configuration";
              homepage = "https://github.com/rawkode/cuenv";
              license = licenses.mit;
              maintainers = [ ];
            };
          };

        in
        {
          packages = {
            default = cuenv;
            cuenv = cuenv;
          };

          # Comprehensive checks for nix flake check
          checks =
            let
              # Vendor cargo dependencies so checks can run offline
              cargoVendor = pkgs.rustPlatform.fetchCargoVendor {
                src = ./.;
                name = "cuenv-cargo-vendor";
                hash = "sha256-FI3CT29lnnKU0lfiUAJ3mK67WQjPNGvGql+NdKlbDBE=";
              };

              # Common preBuild steps for checks
              checksPreBuild = ''
                export HOME=$(mktemp -d)
                export GOPATH="$HOME/go"
                export GOCACHE="$HOME/go-cache"
                export CGO_ENABLED=1

                # Copy Go vendor
                cp -r ${goVendor}/vendor libcue-bridge/
                chmod -R u+w libcue-bridge

                # Setup cargo vendor for offline build
                mkdir -p .cargo
                cat > .cargo/config.toml <<EOF
                [source.crates-io]
                replace-with = "vendored-sources"

                [source.vendored-sources]
                directory = "vendor"
                EOF

                cp -r ${cargoVendor} vendor
                chmod -R u+w vendor
              '';
            in
            {
              # Formatting check
              formatting = treefmt.config.build.check self;

              # Build check - ensure the package builds (without tests)
              build = cuenv;

              # Clippy check
              clippy = pkgs.stdenv.mkDerivation {
                pname = "cuenv-clippy-check";
                version = version;
                src = ./.;

                nativeBuildInputs = nativeBuildInputs;
                buildInputs = buildInputs;

                preBuild = checksPreBuild;

                buildPhase = ''
                  runHook preBuild
                  cargo clippy --all-targets --all-features -- -D warnings \
                    -A clippy::duplicate_mod \
                    -A clippy::uninlined_format_args \
                    -A clippy::too_many_arguments
                  runHook postBuild
                '';

                installPhase = "touch $out";
              };

              # Unit tests (sandbox-compatible)
              unit-tests = pkgs.stdenv.mkDerivation {
                pname = "cuenv-unit-tests";
                version = version;
                src = ./.;

                nativeBuildInputs = nativeBuildInputs ++ [ pkgs.cargo-nextest ];
                buildInputs = buildInputs;

                preBuild = checksPreBuild;

                buildPhase = ''
                  export RUST_TEST_THREADS=2
                  export GOMAXPROCS=2
                  runHook preBuild
                  # Run only unit tests that work in sandbox
                  cargo nextest run --lib --bins \
                    -E 'not test(/concurrent|thread_safe|monitored_cache|profiling|tree_operations|confidence|sequential_pattern|streaming|prop_test_cache|statistics|parse_shell|process_guard/)'
                  runHook postBuild
                '';

                installPhase = "touch $out";
              };

              # Integration tests (requires network, runs in CI only)
              integration-tests = pkgs.stdenv.mkDerivation {
                pname = "cuenv-integration-tests";
                version = version;
                src = ./.;

                nativeBuildInputs = nativeBuildInputs ++ [ pkgs.cargo-nextest ];
                buildInputs = buildInputs;

                preBuild = ''
                  export HOME=$(mktemp -d)
                  export GOPATH="$HOME/go"
                  export GOCACHE="$HOME/go-cache"
                  export CGO_ENABLED=1
                  export RUST_TEST_THREADS=4
                  export GOMAXPROCS=4

                  cp -r ${goVendor}/vendor libcue-bridge/
                  chmod -R u+w libcue-bridge
                '';

                buildPhase = ''
                  runHook preBuild
                  # This will fail in sandbox but documents what tests need network
                  echo "Integration tests require network access and should be run in CI"
                  echo "Would run: cargo nextest run --tests"
                  runHook postBuild
                '';

                installPhase = "touch $out";
              };
            };

          # Make formatter available
          formatter = treefmt.config.build.wrapper;

          devShells.default = pkgs.mkShell {
            buildInputs = buildInputs ++ nativeBuildInputs ++ devTools;

            shellHook = ''
              if [ -t 2 ]; then
                echo "cuenv development environment" >&2
                echo "Rust version: $(rustc --version)" >&2
                echo "Go version: $(go version)" >&2
                echo "" >&2
                echo "Available commands:" >&2
                echo "  cargo build    - Build the project" >&2
                echo "  cargo test     - Run tests" >&2
                echo "  cargo nextest run - Run tests with nextest (faster)" >&2
                echo "  cargo run      - Run cuenv" >&2
                echo "  cargo watch    - Watch for changes and rebuild" >&2
                echo "  treefmt        - Format all code" >&2
                echo "  nix flake check - Check code formatting" >&2
              fi

              # Set up environment for building
              export CGO_ENABLED=1
              export GOPATH="$HOME/go"
              export GOCACHE="$HOME/.cache/go-build"

              # Platform-specific setup
              ${
                if pkgs.stdenv.isDarwin then
                  ''
                    export RUSTFLAGS="-C link-arg=-framework -C link-arg=Security -C link-arg=-framework -C link-arg=CoreFoundation"
                  ''
                else
                  ""
              }
            '';
          };
        }
      )) // {
      # Home Manager module (using standard flake schema to avoid warnings)
      homeManagerModule = { config, lib, pkgs, ... }:
        with lib;
        let
          cfg = config.programs.cuenv;
        in
        {
          options.programs.cuenv = {
            enable = mkEnableOption "cuenv, a direnv alternative using CUE files";

            package = mkOption {
              type = types.package;
              default = self.packages.${pkgs.system}.default;
              defaultText = literalExpression "cuenv";
              description = "The cuenv package to use.";
            };

            enableBashIntegration = mkOption {
              type = types.bool;
              default = config.programs.bash.enable;
              defaultText = literalExpression "config.programs.bash.enable";
              description = ''
                Whether to enable Bash integration.
              '';
            };

            enableZshIntegration = mkOption {
              type = types.bool;
              default = config.programs.zsh.enable;
              defaultText = literalExpression "config.programs.zsh.enable";
              description = ''
                Whether to enable Zsh integration.
              '';
            };

            enableFishIntegration = mkOption {
              type = types.bool;
              default = config.programs.fish.enable;
              defaultText = literalExpression "config.programs.fish.enable";
              description = ''
                Whether to enable Fish integration.
              '';
            };

            enableNushellIntegration = mkOption {
              type = types.bool;
              default = config.programs.nushell.enable;
              defaultText = literalExpression "config.programs.nushell.enable";
              description = ''
                Whether to enable Nushell integration.

                Note: Nushell support is experimental and may require manual configuration.
              '';
            };

            enableBashCompletion = mkOption {
              type = types.bool;
              default = cfg.enableBashIntegration;
              defaultText = literalExpression "cfg.enableBashIntegration";
              description = ''
                Whether to enable Bash completion for cuenv.
              '';
            };

            enableZshCompletion = mkOption {
              type = types.bool;
              default = cfg.enableZshIntegration;
              defaultText = literalExpression "cfg.enableZshIntegration";
              description = ''
                Whether to enable Zsh completion for cuenv.
              '';
            };

            enableFishCompletion = mkOption {
              type = types.bool;
              default = cfg.enableFishIntegration;
              defaultText = literalExpression "cfg.enableFishIntegration";
              description = ''
                Whether to enable Fish completion for cuenv.
              '';
            };
          };

          config = mkIf cfg.enable {
            home.packages = [ cfg.package ];

            programs.bash.initExtra = mkIf cfg.enableBashIntegration ''
              # cuenv shell integration
              eval "$(${cfg.package}/bin/cuenv init bash)"
            '';

            programs.bash.bashrcExtra = mkIf cfg.enableBashCompletion ''
              # cuenv completion
              if command -v cuenv >/dev/null 2>&1; then
                eval "$(${cfg.package}/bin/cuenv completion bash)"
              fi
            '';

            programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
              # cuenv shell integration
              eval "$(${cfg.package}/bin/cuenv init zsh)"
            '';

            programs.zsh.completionInit = mkIf cfg.enableZshCompletion ''
              # cuenv completion
              if command -v cuenv >/dev/null 2>&1; then
                eval "$(${cfg.package}/bin/cuenv completion zsh)"
              fi
            '';

            programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration ''
              # cuenv shell integration
              ${cfg.package}/bin/cuenv init fish | source
            '';

            programs.fish.shellInit = mkIf cfg.enableFishCompletion ''
              # cuenv completion
              if command -v cuenv >/dev/null 2>&1
                ${cfg.package}/bin/cuenv completion fish | source
              end
            '';

            programs.nushell.extraConfig = mkIf cfg.enableNushellIntegration ''
              # cuenv shell integration
              # Note: This is experimental and may need adjustment based on your Nushell version
              let cuenv_init = (${cfg.package}/bin/cuenv init nushell | str trim)
              if not ($cuenv_init | is-empty) {
                source-env { $cuenv_init | from nuon }
              }
            '';
          };
        };
    };
}