File size: 7,129 Bytes
29ed697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543c8e8
 
29ed697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c9a58f
 
543c8e8
 
29ed697
 
 
 
 
 
918c06e
 
 
29ed697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
918c06e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18d65b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29ed697
 
18d65b9
 
 
29ed697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
918c06e
29ed697
 
 
 
 
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
{
  description = "Nanobrush development and Microbrush reference preservation";

  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-26.05";
  inputs.parts.url = "github:hercules-ci/flake-parts";
  inputs.my-nix = {
    url = "github:nmrshll/my-nix";
    inputs.nixpkgs.follows = "nixpkgs";
    inputs.fp.follows = "parts";
  };

  outputs =
    inputs@{ parts, ... }:
    parts.lib.mkFlake { inherit inputs; } (
      { ... }: {
        systems = [
          "aarch64-darwin"
          "x86_64-darwin"
          "x86_64-linux"
          "aarch64-linux"
        ];
        imports =
          builtins.attrValues (inputs.my-nix.flakeModuleGroups or inputs.my-nix.flakeModules).essentials
          ++ [ inputs.my-nix.flakeModules.rust ];
        perSystem =
          {
            pkgs,
            lib,
            config,
            ...
          }:
          let
            python = pkgs.python3.withPackages (ps: [
              ps.huggingface-hub
              ps.hf-xet
            ]);
            runtimeDeps = lib.optionals pkgs.stdenv.isLinux (
              with pkgs;
              [
                alsa-lib
                udev
                vulkan-loader
                wayland
                libxkbcommon
                libX11
                libXcursor
                libXi
                libXrandr
              ]
            );
            devTools = with pkgs; [
              git
              git-lfs
              git-xet
              python
              unzip
              zip
              ripgrep
              jq
              coreutils
              nixfmt
              shellcheck
              pkg-config
              cmake
              ninja
              cargo-nextest
              ilspycmd
              mono
            ];
            command = script: ''
              set -eu
              export PATH=${
                lib.makeBinPath (
                  [
                    python
                    pkgs.git
                    pkgs.git-lfs
                    pkgs.git-xet
                  ]
                  ++ devTools
                )
              }:$PATH
              cd "$(git rev-parse --show-toplevel)"
              exec ${python}/bin/python3 ${script} "$@"
            '';
            scripts = builtins.mapAttrs pkgs.writeShellScriptBin {
              setup-repository = command "scripts/setup-repository.py";
              check-storage = command "scripts/storage_policy.py check --require-setup";
              assets-check = command "scripts/assets.py check";
              assets-fetch = command "scripts/assets.py fetch";
              verify-references = command "scripts/references.py verify";
              unpack-references = command "scripts/references.py unpack";
              index-references = command "scripts/references.py index";
              map-references = command "scripts/map_references.py";
              check-reference-map = command "scripts/map_references.py --check";
              analyze-converter = command "scripts/analyze_converter.py";
              inspect-mb3dat = command "scripts/inspect_mb3dat.py";
              test-setup = command "scripts/test_setup.py";
              check-environment = command "scripts/check_environment.py";
            };
            codeSource = lib.fileset.toSource {
              root = ./.;
              fileset = lib.fileset.unions [
                ./Cargo.toml
                ./Cargo.lock
                ./crates
                ./scripts
                ./tests
                ./config
                ./docs
                ./.githooks
                ./README.md
                ./.gitattributes
                ./.gitignore
                ./flake.nix
                ./flake.lock
                ./.envrc
              ];
            };
            setupCheck =
              pkgs.runCommand "nanobrush-setup-checks"
                {
                  nativeBuildInputs = [
                    pkgs.python3
                    pkgs.git
                    pkgs.git-lfs
                    pkgs.git-xet
                  ];
                }
                ''
                  cp -R ${codeSource}/. .
                  chmod -R u+w .
                  python3 scripts/test_setup.py
                  python3 scripts/storage_policy.py check-tree ${codeSource}
                  touch $out
                '';
            rustCheck =
              pkgs.runCommand "nanobrush-rust-checks"
                {
                  nativeBuildInputs = [
                    config.rust.toolchain
                    pkgs.stdenv.cc
                  ];
                }
                ''
                  cp -R ${codeSource}/. .
                  chmod -R u+w .
                  export CARGO_HOME="$TMPDIR/nanobrush-cargo"
                  cargo fmt --all --check
                  cargo clippy --workspace --all-targets --locked --offline -- -D warnings
                  cargo test --workspace --lib --locked --offline
                  cargo test --workspace --doc --locked --offline
                  touch $out
                '';
            cliPackage =
              pkgs.runCommand "nanobrush-cli-0.1.0"
                {
                  nativeBuildInputs = [
                    config.rust.toolchain
                    pkgs.stdenv.cc
                  ];
                  meta.mainProgram = "nanobrush";
                }
                ''
                  cp -R ${codeSource}/. .
                  chmod -R u+w .
                  export CARGO_HOME="$TMPDIR/nanobrush-cargo"
                  cargo build -p nanobrush-cli --release --locked --offline
                  mkdir -p "$out/bin"
                  cp target/release/nanobrush "$out/bin/nanobrush"
                '';
          in
          {
            packages = scripts // {
              nanobrush = cliPackage;
            };
            rust.buildInputs = runtimeDeps;
            rust.nativeBuildInputs = [ pkgs.pkg-config ];
            rust.toolchain = pkgs.rust-bin.stable.latest.default.override {
              extensions = [
                "rust-src"
                "rust-analyzer"
                "rustfmt"
                "clippy"
              ];
            };
            myDevShell.overrides.stdenv = pkgs.stdenv;
            myDevShell.buildInputs = lib.mkForce (
              [ config.rust.toolchain ] ++ devTools ++ runtimeDeps ++ builtins.attrValues scripts
            );
            myDevShell.env = {
              RUST_BACKTRACE = "1";
            }
            // lib.optionalAttrs pkgs.stdenv.isLinux { LD_LIBRARY_PATH = lib.makeLibraryPath runtimeDeps; };
            # Suppress shared editor/global configuration hooks; only configure this checkout.
            myDevShell.shellHooks = lib.mkForce {
              repository = ''
                if [ -f scripts/setup-repository.py ]; then
                  ${python}/bin/python3 scripts/setup-repository.py || return 1
                fi
              '';
            };
            checks.setup = setupCheck;
            checks.rust = rustCheck;
            formatter = pkgs.nixfmt;
          };
      }
    );
}