Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
csv2journal flake.nix
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177{ description = "Convert bank CSV exports into hledger journals";
inputs = { # M1-PKG-001 requires Zig 0.17.0. nixos-unstable carries it as zig_0_17; # the stable channels do not. nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# `tang`, the CLI for tangled.org, where this repository is hosted. # Upstream ships its own flake; take its package rather than repackaging # it, and pin it to our nixpkgs so the shell pulls in one of them, not two. tangled-cli = { url = "git+https://tangled.org/dzejkop.space/tangled-cli"; inputs.nixpkgs.follows = "nixpkgs"; }; };
outputs = { self, nixpkgs, tangled-cli, ... }: let systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; forAllSystems = f: nixpkgs.lib.genAttrs systems (s: f nixpkgs.legacyPackages.${s});
# A check that runs `script` in the application's source tree, with Zig # pointed at a writable cache and handed the dependencies Nix fetched. # `script` takes that dependency directory; `inputs` are further tools # the script calls. zigCheck = pkgs: { pname, script, inputs ? [ ], }: self.packages.${pkgs.stdenv.hostPlatform.system}.default.overrideAttrs (old: { inherit pname; nativeBuildInputs = old.nativeBuildInputs ++ inputs; buildPhase = '' runHook preBuild
export HOME="$TMPDIR" export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-global-cache" mkdir -p "$ZIG_GLOBAL_CACHE_DIR"
${script old.zigDeps} touch "$out"
runHook postBuild ''; }); in { # M1-PKG-002: the flake builds the application ... packages = forAllSystems (pkgs: { default = pkgs.callPackage ./nix/package.nix { }; });
# ... and runs its automated tests. checks = forAllSystems ( pkgs: { # `zig build test` never analyses main(), so a broken entry point only # surfaces when the executable is actually linked. Build it as a check. build = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
unit = zigCheck pkgs { pname = "csv2journal-tests"; script = deps: '' zig build test --system "${deps}" ''; };
# `zig build lint` runs zlinter over the sources. zlinter runs a # `zig build` of its own to read the build configuration, without # our --system, so the packages have to reach it through the # environment instead. lint = zigCheck pkgs { pname = "csv2journal-lint"; script = deps: '' export ZIG_LOCAL_PKG_DIR="${deps}" zig build lint --system "${deps}" ''; };
# The interface's JavaScript tests (D1). ui-test = zigCheck pkgs { pname = "csv2journal-ui-tests"; inputs = [ pkgs.deno ]; script = deps: '' zig build test-ui --system "${deps}" ''; };
# The interface's JavaScript and CSS (D1). ui-lint = zigCheck pkgs { pname = "csv2journal-ui-lint"; inputs = [ pkgs.deno pkgs.biome ]; script = deps: '' zig build lint-ui --system "${deps}" ''; }; } # M1-JRN-008: the compatibility suite, against a pinned hledger # 1.52.3. Upstream publishes an x86_64 Linux binary only, so the check # exists on that system alone rather than failing to evaluate on the # others. // nixpkgs.lib.optionalAttrs (pkgs.stdenv.hostPlatform.system == "x86_64-linux") { hledger = zigCheck pkgs { pname = "csv2journal-hledger-compat"; inputs = [ (pkgs.callPackage ./nix/hledger.nix { }) ]; script = deps: '' # The corpus covers text well outside ASCII, which hledger can # only read under a UTF-8 locale. export LANG=C.UTF-8 export LC_ALL=C.UTF-8
corpus="$TMPDIR/corpus" zig build compat-corpus --system "${deps}" -- "$corpus"
# M1-JRN-007: hledger must accept everything we emit. for journal in "$corpus"/*.journal; do name="$(basename "$journal" .journal)" echo "hledger parses $name" hledger -f "$journal" print > "$corpus/$name.hledger" done
# And must not have changed it on the way through. The cases # where hledger legitimately disagrees are marked in # src/compat/corpus.zig and skipped there, not here. zig build compat-corpus --system "${deps}" -- --verify "$corpus" ''; }; } );
devShells = forAllSystems (pkgs: { default = pkgs.mkShell { packages = [ pkgs.zig_0_17 # No zls: nixpkgs has no build for Zig 0.17 yet, and a 0.16 zls # would misread the code. pkgs.zon2nix # regenerate nix/deps.nix after editing build.zig.zon # `zig build lint-ui` and `zig build test-ui` (D1). pkgs.deno pkgs.biome tangled-cli.packages.${pkgs.stdenv.hostPlatform.system}.default ]; # Nix fetched every dependency already; hand the ones Zig's global # cache lacks to zig fetch, which files them under the hash # build.zig.zon pins. A plain `zig build` then needs no network and # builds against exactly what the checks use. shellHook = '' zig_cache="''${ZIG_GLOBAL_CACHE_DIR:-''${XDG_CACHE_HOME:-$HOME/.cache}/zig}" for pkg in ${self.packages.${pkgs.stdenv.hostPlatform.system}.default.zigDeps}/*; do if [ ! -e "$zig_cache/p/$(basename "$pkg").tar.gz" ]; then zig fetch "$pkg" > /dev/null fi done ''; }; }); };}