Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 pkgs,
4 formats,
5 runCommand,
6}:
7let
8 inherit (lib)
9 last
10 optionalString
11 types
12 ;
13in
14{
15 /**
16 Creates a transformer function that writes input data to disk, transformed
17 by both the `input` and `output` arguments.
18
19 # Example
20
21 ```nix
22 writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; };
23 myConfig = writeJSON "config.json" { hello = "world"; }
24 ```
25
26 # Type
27
28 ```
29 makeDataWriter :: input -> output -> nameOrPath -> data -> (any -> string) -> string -> string -> any -> derivation
30
31 input :: T -> string: function that takes the nix data and returns a string
32 output :: string: script that takes the $inputFile and write the result into $out
33 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.
34 data :: T: the data that will be converted.
35 ```
36 */
37 makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." (
38 {
39 input ? lib.id,
40 output ? "cp $inputPath $out",
41 }:
42 nameOrPath: data:
43 assert
44 (types.path.check nameOrPath)
45 || (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
46 let
47 name = last (builtins.split "/" nameOrPath);
48 in
49 runCommand name
50 {
51 input = input data;
52 passAsFile = [ "input" ];
53 }
54 ''
55 ${output}
56
57 ${optionalString (types.path.check nameOrPath) ''
58 mv $out tmp
59 mkdir -p $out/$(dirname "${nameOrPath}")
60 mv tmp $out/${nameOrPath}
61 ''}
62 ''
63 );
64
65 inherit (pkgs) writeText;
66
67 /**
68 Writes the content to a JSON file.
69
70 # Example
71
72 ```nix
73 writeJSON "data.json" { hello = "world"; }
74 ```
75 */
76 writeJSON = (pkgs.formats.json { }).generate;
77
78 /**
79 Writes the content to a TOML file.
80
81 # Example
82
83 ```nix
84 writeTOML "data.toml" { hello = "world"; }
85 ```
86 */
87 writeTOML = (pkgs.formats.toml { }).generate;
88
89 /**
90 Writes the content to a YAML file.
91
92 # Example
93
94 ```nix
95 writeYAML "data.yaml" { hello = "world"; }
96 ```
97 */
98 writeYAML = (pkgs.formats.yaml { }).generate;
99}