nix-csv/flake.nix

78 lines
1.8 KiB
Nix

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, ... }: {
packages = nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" ] (system: let
pkgs = import nixpkgs {
inherit system;
};
/*
This is a helper script that converts a CSV to a JSON file
Call it with:
nix run .#csv-to-json -- test.csv test.json
*/
csv-to-json = pkgs.writers.writePython3Bin "csv-to-json" {} ''
import json
from pathlib import Path
import sys
csv_file = Path(sys.argv[1])
json_file = Path(sys.argv[2])
json_file.write_text(json.dumps([
line.split(",") for line in csv_file.read_text().splitlines()]) + "\n")
'';
/*
This functions returns a json file for the specified csv file
*/
convert-to-json = csv-file: pkgs.runCommand "convert-file" {
src = csv-file;
} ''
${csv-to-json}/bin/csv-to-json $src $out
'';
/*
Out csv data sits under ./test.csv
*/
csv-file = ./test.csv;
/*
We convert the csv file to a json file
*/
json-file = convert-to-json csv-file;
/*
We load the data from the json file and can access it like any other nix var
*/
data = builtins.fromJSON (builtins.readFile json-file);
in {
inherit csv-to-json;
/*
This is a sample derivation that takes data and writes the first column of the csv to a file
Run like:
nix build .#nix-csv
See output:
cat result
*/
nix-csv = pkgs.writeText "nix-csv" (pkgs.lib.concatMapStringsSep "\n" (row: builtins.head row) data);
});
hydraJobs = {
inherit (self)
packages;
};
};
}