commit be06e4989ad1226bd26c4a0a4e1d29ae30455cd6
Author: clerie <git@clerie.de>
Date:   Tue Feb 20 17:11:28 2024 +0100

    Init repo

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..fcfc4a1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+result*
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..02e42e2
--- /dev/null
+++ b/LICENSE
@@ -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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..16d89bf
--- /dev/null
+++ b/README.md
@@ -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.
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..a73a958
--- /dev/null
+++ b/flake.lock
@@ -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
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..3915702
--- /dev/null
+++ b/flake.nix
@@ -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;
+    };
+  };
+}
diff --git a/test.csv b/test.csv
new file mode 100644
index 0000000..7a8bd5f
--- /dev/null
+++ b/test.csv
@@ -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