quilt/langs/nix/meta.rs
1//! The Nix meta-language: Nix as a *host* that drives generation.
2//!
3//! A `.nix.quilt` file is a Nix metaprogram. Where the Rust/Python hosts emit
4//! builder calls into a `QTerm` runtime, this host has **no runtime library**:
5//! it represents generated code as plain Nix strings (see [`super::ops`]). A
6//! quote `↖ … ↗` expands to a Nix string literal and a host unquote `↙x↘` to
7//! Nix's own `${x}` antiquotation, so evaluating the expanded program (e.g.
8//! with `nix eval`) yields the generated code as a string.
9//!
10//! Because everything is a string, a Nix host can generate *any* target
11//! language — `bash↖…↗`, `nix↖…↗`, … all reconstruct the same way.
12
13use miette::Result;
14
15use super::ops::{build_quote_str, build_str_code, build_unquote_str};
16use crate::lang::Arity;
17use crate::meta::OuterKind;
18use crate::prelude::{Index, *};
19use crate::{meta::MetaLanguage, qterm::QTerm, term::CmdOrHole};
20
21/**************************************************************/
22
23#[derive(Default)]
24pub struct NixMetaLanguage;
25
26impl MetaLanguage for NixMetaLanguage {
27 fn expand_tuple(
28 &self,
29 _lang1: &str,
30 _tag: &str,
31 qterms: &[Arc<QTerm>],
32 cmds: &[CmdOrHole],
33 _arity: Arity,
34 ) -> Result<Arc<QTerm>> {
35 // Strings have no builder/accumulator, so variadic and fixed nodes
36 // reconstruct identically: literal text plus spliced children.
37 Ok(build_str_code(cmds, qterms))
38 }
39
40 fn expand_quote(
41 &self,
42 _lang1: &str,
43 _tag: &str,
44 _i: Index,
45 lang2: &str,
46 qterm: &Arc<QTerm>,
47 _cmds: &[CmdOrHole],
48 ) -> Result<Arc<QTerm>> {
49 Ok(build_quote_str(lang2, qterm))
50 }
51
52 fn expand_unquote(
53 &self,
54 _lang1: &str,
55 _tag: &str,
56 _i: Index,
57 lang2: &str,
58 qterm: &Arc<QTerm>,
59 _cmds: &[CmdOrHole],
60 ) -> Result<Arc<QTerm>> {
61 Ok(build_unquote_str(lang2, qterm))
62 }
63
64 /// Identity: the string model has no `b_` accumulator to emit/splice into,
65 /// so a child is woven into its parent purely by `expand_tuple`'s
66 /// concatenation. A child that is *itself* a sequence goes through the `←`
67 /// operator instead of this hook — see [`Self::emit_str`].
68 fn wrap_child(&self, qterm: Arc<QTerm>, _okind: OuterKind) -> Result<Arc<QTerm>> {
69 Ok(qterm)
70 }
71
72 /// `↑` renders a host value as text for interpolation. There is no runtime
73 /// `QTerm`, so the only spelling is Nix's own `toString`; cross-language
74 /// lifts have no spelling.
75 fn lift_str(&self, target: &str) -> Result<&'static str> {
76 match target {
77 "" | "nix" => Ok("toString"),
78 _ => miette::bail!("nix can't lift into {target:?}: only homogeneous `toString`"),
79 }
80 }
81
82 /// `←` appends a *sequence* of fragments into the surrounding container.
83 ///
84 /// The imperative reading of emit — append one term to a `b_` accumulator,
85 /// once per loop iteration — has no counterpart in Nix: it is a pure
86 /// expression language with no statements and nothing to mutate. Its
87 /// functional counterpart does: build the list of fragments with `map` and
88 /// hand the whole list to `←`, which joins it into one fragment. So
89 ///
90 /// ```nix
91 /// nix↖[ ↙← (map (n: nix↖"↙n↘"↗) names)↘ ]↗
92 /// ```
93 ///
94 /// is the string model's counterpart of a runtime-backed host's `↖…↗.←`
95 /// inside a ground loop. The spelling is `builtins`-only (this host ships
96 /// no runtime library) and is applied prefix, by juxtaposition, like
97 /// `↑`/`toString`: `← xs`, not `xs.←`.
98 ///
99 /// The separator is a newline because it is the only one that is *correct*
100 /// for every container: Nix is whitespace-insensitive, so `[ a\nb ]` and
101 /// `{ inherit a\nb; }` are the lists the source meant, and a generated
102 /// line-oriented target (bash, python, …) gets one statement per line
103 /// rather than a run-on. Where a different separator is wanted (a
104 /// comma-separated `formals`, say), call `builtins.concatStringsSep`
105 /// directly — `←` is exactly its newline partial application.
106 fn emit_str(&self) -> Result<&'static str> {
107 Ok("(builtins.concatStringsSep \"\\n\")")
108 }
109
110 /// No spelling: `↓` compiles a term and deserializes the result back, which
111 /// needs the `QTerm` runtime this host doesn't have. Generation-time
112 /// evaluation is ordinary Nix — compute the value in a `let` and splice it
113 /// with `↙…↘`.
114 fn reduce_str(&self, target: &str) -> Result<&'static str> {
115 miette::bail!(
116 "nix can't reduce `{target}↓`: the string-based meta has no `QTerm` runtime to \
117 evaluate a fragment — compute the value in ordinary Nix and splice it with `↙…↘`"
118 )
119 }
120
121 /// No spelling: Nix is untyped and has no annotation syntax, so there is
122 /// nowhere for `⟨T⟩` to go. (Lean, the other string host, answers `String`.)
123 fn type_str(&self) -> Result<&'static str> {
124 miette::bail!(
125 "nix has no type for `⟨T⟩`: Nix is untyped, and a generated fragment is just a \
126 string — drop the annotation"
127 )
128 }
129
130 /// In the string model a name is its own text, so `⟨N⟩` is the identity —
131 /// which for a Nix string is what `toString` already does.
132 fn name_str(&self) -> Result<&'static str> {
133 Ok("toString")
134 }
135}