about summary refs log tree commit diff
path: root/nixpkgs/pkgs/build-support/writers/data.nix
blob: 45ed5360eaebaf4805b839df57bdc26e18cabfa9 (plain) (blame)
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
{ lib, pkgs, formats, runCommand, dasel }:
let
  daselBin = lib.getExe dasel;

  inherit (lib)
    last
    optionalString
    types
    ;
in
rec {
  # Creates a transformer function that writes input data to disk, transformed
  # by both the `input` and `output` arguments.
  #
  # Type: makeDataWriter :: input -> output -> nameOrPath -> data -> (any -> string) -> string -> string -> any -> derivation
  #
  #   input :: T -> string: function that takes the nix data and returns a string
  #   output :: string: script that takes the $inputFile and write the result into $out
  #   nameOrPath :: string: if the name contains a / the files gets written to a sub-folder of $out. The derivation name is the basename of this argument.
  #   data :: T: the data that will be converted.
  #
  # Example:
  #   writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; };
  #   myConfig = writeJSON "config.json" { hello = "world"; }
  #
  makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data:
    assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
    let
      name = last (builtins.split "/" nameOrPath);
    in
    runCommand name
      {
        input = input data;
        passAsFile = [ "input" ];
      } ''
      ${output}

      ${optionalString (types.path.check nameOrPath) ''
        mv $out tmp
        mkdir -p $out/$(dirname "${nameOrPath}")
        mv tmp $out/${nameOrPath}
      ''}
    '');

  inherit (pkgs) writeText;

  # Writes the content to a JSON file.
  #
  # Example:
  #   writeJSON "data.json" { hello = "world"; }
  writeJSON = (pkgs.formats.json {}).generate;

  # Writes the content to a TOML file.
  #
  # Example:
  #   writeTOML "data.toml" { hello = "world"; }
  writeTOML = (pkgs.formats.toml {}).generate;

  # Writes the content to a YAML file.
  #
  # Example:
  #   writeYAML "data.yaml" { hello = "world"; }
  writeYAML = (pkgs.formats.yaml {}).generate;
}