Init repo
This commit is contained in:
commit
be06e4989a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
result*
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 clerie
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
16
README.md
Normal file
16
README.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# nix-csv
|
||||||
|
|
||||||
|
Load a csv and use it in your nix as an attribute.
|
||||||
|
|
||||||
|
The challenge here is, that you could -in theory- parse files in nix directly, but nix is really bad at this.
|
||||||
|
What we do here, is using [builtins.fromJSON](https://nixos.org/manual/nix/unstable/language/builtins#builtins-fromJSON) by just converting the csv file to a json file first.
|
||||||
|
|
||||||
|
Quick summary of the pipeline:
|
||||||
|
|
||||||
|
```
|
||||||
|
test.csv -> csv-to-json -> test.json -> readFile -> fromJSON
|
||||||
|
```
|
||||||
|
|
||||||
|
See comments in `flake.nix` for how it works.
|
||||||
|
|
||||||
|
Technically this can adapted to any type of data source.
|
27
flake.lock
Normal file
27
flake.lock
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1708296515,
|
||||||
|
"narHash": "sha256-FyF489fYNAUy7b6dkYV6rGPyzp+4tThhr80KNAaF/yY=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "b98a4e1746acceb92c509bc496ef3d0e5ad8d4aa",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
77
flake.nix
Normal file
77
flake.nix
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
{
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
16
test.csv
Normal file
16
test.csv
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Stuttgart,Land Baden-Württemberg
|
||||||
|
München,Freistaat Bayern
|
||||||
|
Berlin,Land Berlin
|
||||||
|
Potsdam,Land Brandenburg
|
||||||
|
Bremen,Freie Hansestadt Bremen
|
||||||
|
Hamburg,Freie und Hansestadt Hamburg
|
||||||
|
Wiesbaden,Land Hessen
|
||||||
|
Schwerin,Land Mecklenburg-Vorpommern
|
||||||
|
Hannover,Land Niedersachsen
|
||||||
|
Düsseldorf,Land Nordrhein-Westfalen
|
||||||
|
Mainz,Land Rheinland-Pfalz
|
||||||
|
Saarbrücken,Saarland
|
||||||
|
Dresden,Freistaat Sachsen
|
||||||
|
Magdeburg,Land Sachsen-Anhalt
|
||||||
|
Kiel,Land Schleswig-Holstein
|
||||||
|
Erfurt,Freistaat Thüringen
|
|
Loading…
Reference in New Issue
Block a user