sui-compat 0.1.83

Clean-room Nix format and protocol compatibility layer (NAR, store paths, ATerm, derivations)
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! Nix derivation (.drv) ATerm format — parse/serialize.
//!
//! Clean recursive-descent parser using a `Cursor` abstraction over the input.
//! The ATerm format is simple enough that parser combinators are unnecessary.

use std::collections::BTreeMap;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum DerivationError {
    #[error("parse error at byte {pos}: {message}")]
    Parse { pos: usize, message: String },
    #[error("unexpected end of input")]
    UnexpectedEof,
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
}

/// A derivation output descriptor.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DerivationOutput {
    /// Store path for this output (empty for floating content-addressed outputs).
    pub path: String,
    /// Hash algorithm (e.g. `"sha256"`), empty for input-addressed outputs.
    pub hash_algo: String,
    /// Expected hash value, empty for input-addressed outputs.
    pub hash: String,
}

/// A parsed Nix derivation.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Derivation {
    /// Named outputs (e.g. `"out"`, `"dev"`, `"lib"`).
    pub outputs: BTreeMap<String, DerivationOutput>,
    /// Input derivations: maps `.drv` store path to the list of outputs used.
    pub input_derivations: BTreeMap<String, Vec<String>>,
    /// Input source store paths (non-derivation dependencies).
    pub input_sources: Vec<String>,
    /// Target system triple (e.g. `"x86_64-linux"`).
    pub system: String,
    /// Path to the builder executable.
    pub builder: String,
    /// Arguments passed to the builder.
    pub args: Vec<String>,
    /// Environment variables set during the build.
    pub env: BTreeMap<String, String>,
}

// ── Cursor ───────────────────────────────────────────────────

/// Simple cursor over a byte slice for zero-copy parsing.
struct Cursor<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> Cursor<'a> {
    fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    fn peek(&self) -> Result<u8, DerivationError> {
        self.data.get(self.pos).copied().ok_or(DerivationError::UnexpectedEof)
    }

    fn advance(&mut self) { self.pos += 1; }

    fn expect(&mut self, ch: u8) -> Result<(), DerivationError> {
        let got = self.peek()?;
        if got != ch {
            return Err(DerivationError::Parse {
                pos: self.pos,
                message: format!("expected '{}', got '{}'", ch as char, got as char),
            });
        }
        self.advance();
        Ok(())
    }

    fn expect_str(&mut self, s: &[u8]) -> Result<(), DerivationError> {
        for &ch in s {
            self.expect(ch)?;
        }
        Ok(())
    }

    /// Parse a quoted string with escape handling.
    fn string(&mut self) -> Result<String, DerivationError> {
        self.expect(b'"')?;
        let mut result = Vec::new();
        loop {
            let ch = self.peek()?;
            self.advance();
            match ch {
                b'"' => return String::from_utf8(result).map_err(|e| DerivationError::Parse {
                    pos: self.pos, message: format!("invalid UTF-8: {e}"),
                }),
                b'\\' => {
                    let esc = self.peek()?;
                    self.advance();
                    match esc {
                        b'n' => result.push(b'\n'),
                        b'r' => result.push(b'\r'),
                        b't' => result.push(b'\t'),
                        b'\\' => result.push(b'\\'),
                        b'"' => result.push(b'"'),
                        _ => { result.push(b'\\'); result.push(esc); }
                    }
                }
                _ => result.push(ch),
            }
        }
    }

    /// Parse `[item, item, ...]` using the given item parser.
    fn list<T>(&mut self, parse_item: impl Fn(&mut Self) -> Result<T, DerivationError>) -> Result<Vec<T>, DerivationError> {
        self.expect(b'[')?;
        let mut items = Vec::new();
        if self.peek()? != b']' {
            items.push(parse_item(self)?);
            while self.peek()? == b',' {
                self.advance();
                items.push(parse_item(self)?);
            }
        }
        self.expect(b']')?;
        Ok(items)
    }
}

impl Derivation {
    /// Parse a derivation from its ATerm bytes.
    pub fn parse(input: &[u8]) -> Result<Self, DerivationError> {
        let mut c = Cursor::new(input);
        c.expect_str(b"Derive(")?;

        let outputs_list = c.list(|c| {
            c.expect(b'(')?;
            let name = c.string()?;
            c.expect(b',')?;
            let path = c.string()?;
            c.expect(b',')?;
            let hash_algo = c.string()?;
            c.expect(b',')?;
            let hash = c.string()?;
            c.expect(b')')?;
            Ok((name, DerivationOutput { path, hash_algo, hash }))
        })?;
        c.expect(b',')?;

        let input_drvs_list = c.list(|c| {
            c.expect(b'(')?;
            let path = c.string()?;
            c.expect(b',')?;
            let outputs = c.list(|c| c.string())?;
            c.expect(b')')?;
            Ok((path, outputs))
        })?;
        c.expect(b',')?;

        let input_sources = c.list(|c| c.string())?;
        c.expect(b',')?;
        let system = c.string()?;
        c.expect(b',')?;
        let builder = c.string()?;
        c.expect(b',')?;
        let args = c.list(|c| c.string())?;
        c.expect(b',')?;

        let env_list = c.list(|c| {
            c.expect(b'(')?;
            let key = c.string()?;
            c.expect(b',')?;
            let value = c.string()?;
            c.expect(b')')?;
            Ok((key, value))
        })?;

        c.expect(b')')?;

        Ok(Derivation {
            outputs: outputs_list.into_iter().collect(),
            input_derivations: input_drvs_list.into_iter().collect(),
            input_sources,
            system,
            builder,
            args,
            env: env_list.into_iter().collect(),
        })
    }

    /// Serialize the derivation to ATerm format.
    #[must_use]
    pub fn serialize(&self) -> String {
        let mut out = String::from("Derive(");

        out.push('[');
        out.push_str(&join_comma(self.outputs.iter().map(|(name, o)| {
            format!("({},{},{},{})", escape(name), escape(&o.path), escape(&o.hash_algo), escape(&o.hash))
        })));
        out.push_str("],");

        out.push('[');
        out.push_str(&join_comma(self.input_derivations.iter().map(|(path, outputs)| {
            let outs = join_comma(outputs.iter().map(|o| escape(o)));
            format!("({},[{outs}])", escape(path))
        })));
        out.push_str("],");

        let mut sources: Vec<_> = self.input_sources.iter().collect();
        sources.sort();
        out.push('[');
        out.push_str(&join_comma(sources.iter().map(|s| escape(s))));
        out.push_str("],");

        out.push_str(&escape(&self.system));
        out.push(',');
        out.push_str(&escape(&self.builder));

        out.push_str(",[");
        out.push_str(&join_comma(self.args.iter().map(|a| escape(a))));
        out.push_str("],[");
        out.push_str(&join_comma(self.env.iter().map(|(k, v)| {
            format!("({},{})", escape(k), escape(v))
        })));
        out.push_str("])");
        out
    }

    /// Serialize to ATerm in "modulo" form: each `input_derivations`
    /// key (a `.drv` store path) is replaced by the result of
    /// `resolver(drv_path)` — CppNix's `hashDerivationModulo`
    /// recursion.  Used when computing the hash that becomes the
    /// *dependent* derivation's store path.
    ///
    /// The resolver typically looks up a pre-computed modulo hash
    /// (lowercase hex) in an evaluator-level cache.  If the resolver
    /// returns the drv path unchanged, this is identical to
    /// [`Self::serialize`].
    ///
    /// # Panics
    ///
    /// Does not panic; any resolver-returned string is inserted verbatim.
    #[must_use]
    pub fn serialize_modulo(&self, resolver: impl Fn(&str) -> String) -> String {
        let mut out = String::from("Derive(");

        out.push('[');
        out.push_str(&join_comma(self.outputs.iter().map(|(name, o)| {
            format!("({},{},{},{})", escape(name), escape(&o.path), escape(&o.hash_algo), escape(&o.hash))
        })));
        out.push_str("],");

        // CppNix's hashDerivationModulo keys the input derivations by their
        // MODULO hash and serializes them SORTED BY THAT KEY — not by the
        // original drv path. So resolve every path to its modulo first, then
        // re-sort by the resolved value before emitting. Without the re-sort a
        // derivation with ≥2 input drvs whose modulo order differs from their
        // drv-path order serializes its modulo ATerm in the wrong order, and its
        // output path (and everything transitively above it) diverges from nix —
        // even though its full drv ATerm is byte-identical. Single-input drvs
        // never expose this, which is why it survived until a 2-input stdenv drv.
        let mut resolved_inputs: Vec<(String, &Vec<String>)> = self
            .input_derivations
            .iter()
            .map(|(path, outputs)| (resolver(path), outputs))
            .collect();
        resolved_inputs.sort_by(|a, b| a.0.cmp(&b.0));
        out.push('[');
        out.push_str(&join_comma(resolved_inputs.into_iter().map(|(resolved, outputs)| {
            let outs = join_comma(outputs.iter().map(|o| escape(o)));
            format!("({},[{outs}])", escape(&resolved))
        })));
        out.push_str("],");

        let mut sources: Vec<_> = self.input_sources.iter().collect();
        sources.sort();
        out.push('[');
        out.push_str(&join_comma(sources.iter().map(|s| escape(s))));
        out.push_str("],");

        out.push_str(&escape(&self.system));
        out.push(',');
        out.push_str(&escape(&self.builder));

        out.push_str(",[");
        out.push_str(&join_comma(self.args.iter().map(|a| escape(a))));
        out.push_str("],[");
        out.push_str(&join_comma(self.env.iter().map(|(k, v)| {
            format!("({},{})", escape(k), escape(v))
        })));
        out.push_str("])");
        out
    }
}

/// Join an iterator of string fragments with commas.
fn join_comma(items: impl Iterator<Item = String>) -> String {
    let mut out = String::new();
    for (i, item) in items.enumerate() {
        if i > 0 {
            out.push(',');
        }
        out.push_str(&item);
    }
    out
}

/// Escape a string for ATerm serialization (backslash-escaping special chars).
pub(crate) fn escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            _ => out.push(ch),
        }
    }
    out.push('"');
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    #[test]
    fn serialize_roundtrip() {
        let mut outputs = BTreeMap::new();
        outputs.insert("out".to_string(), DerivationOutput {
            path: "/nix/store/abc-hello".to_string(), hash_algo: String::new(), hash: String::new(),
        });
        let mut env = BTreeMap::new();
        env.insert("name".to_string(), "hello".to_string());
        let drv = Derivation {
            outputs,
            input_derivations: BTreeMap::new(),
            input_sources: vec!["/nix/store/src".to_string()],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec!["-e".to_string()],
            env,
        };
        let s = drv.serialize();
        let parsed = Derivation::parse(s.as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    #[test]
    fn escape_roundtrip() {
        let mut env = BTreeMap::new();
        env.insert("s".to_string(), "line1\nline2\r\ttab\\back\"quote".to_string());
        let drv = Derivation {
            outputs: { let mut m = BTreeMap::new(); m.insert("out".to_string(), DerivationOutput { path: "/out".to_string(), hash_algo: String::new(), hash: String::new() }); m },
            input_derivations: BTreeMap::new(), input_sources: vec![], system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(), args: vec![], env,
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    #[test]
    fn multiple_outputs() {
        let mut outputs = BTreeMap::new();
        for name in ["dev", "lib", "out"] {
            outputs.insert(name.to_string(), DerivationOutput {
                path: format!("/nix/store/{name}"), hash_algo: String::new(), hash: String::new(),
            });
        }
        let drv = Derivation {
            outputs, input_derivations: BTreeMap::new(), input_sources: vec![],
            system: "x86_64-linux".to_string(), builder: "/bin/sh".to_string(), args: vec![], env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.outputs.len(), 3);
    }

    #[test]
    fn multiple_input_drvs() {
        let mut input_drvs = BTreeMap::new();
        input_drvs.insert("/nix/store/a.drv".to_string(), vec!["out".to_string()]);
        input_drvs.insert("/nix/store/b.drv".to_string(), vec!["out".to_string(), "lib".to_string()]);
        let drv = Derivation {
            outputs: { let mut m = BTreeMap::new(); m.insert("out".to_string(), DerivationOutput { path: "/out".to_string(), hash_algo: String::new(), hash: String::new() }); m },
            input_derivations: input_drvs, input_sources: vec![], system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(), args: vec![], env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.input_derivations.len(), 2);
    }

    #[test]
    fn empty_everything() {
        let drv = Derivation {
            outputs: { let mut m = BTreeMap::new(); m.insert("out".to_string(), DerivationOutput { path: "/out".to_string(), hash_algo: String::new(), hash: String::new() }); m },
            input_derivations: BTreeMap::new(), input_sources: vec![], system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(), args: vec![], env: BTreeMap::new(),
        };
        let s = drv.serialize();
        assert!(s.contains(",[],"));
        let parsed = Derivation::parse(s.as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    #[test]
    fn fixed_output_derivation() {
        let mut outputs = BTreeMap::new();
        outputs.insert("out".to_string(), DerivationOutput {
            path: "/nix/store/src.tar.gz".to_string(),
            hash_algo: "sha256".to_string(),
            hash: "1b0ri5lsf45dknj8bfxi1syz35kmab77apxxg1yrf33la1qm3kc7".to_string(),
        });
        let drv = Derivation {
            outputs, input_derivations: BTreeMap::new(), input_sources: vec![],
            system: "x86_64-linux".to_string(), builder: "/bin/curl".to_string(), args: vec!["-o".to_string()], env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed, drv);
        assert_eq!(parsed.outputs["out"].hash_algo, "sha256");
    }

    // ── Many outputs ─────────────────────────────────────

    #[test]
    fn many_outputs_roundtrip() {
        let mut outputs = BTreeMap::new();
        for name in ["bin", "data", "debug", "dev", "doc", "info", "lib", "man", "out", "static"] {
            outputs.insert(name.to_string(), DerivationOutput {
                path: format!("/nix/store/hash-pkg-{name}"),
                hash_algo: String::new(),
                hash: String::new(),
            });
        }
        let drv = Derivation {
            outputs,
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.outputs.len(), 10);
        assert_eq!(parsed, drv);
    }

    // ── Unusual env vars ─────────────────────────────────

    #[test]
    fn env_with_special_characters() {
        let mut env = BTreeMap::new();
        env.insert("multiline".to_string(), "line1\nline2\nline3".to_string());
        env.insert("tabs_and_cr".to_string(), "col1\tcol2\r\n".to_string());
        env.insert("backslash".to_string(), "C:\\Users\\nix".to_string());
        env.insert("quotes".to_string(), r#"say "hello""#.to_string());
        env.insert("empty".to_string(), String::new());

        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(), hash_algo: String::new(), hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env,
        };
        let serialized = drv.serialize();
        let parsed = Derivation::parse(serialized.as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    #[test]
    fn env_with_long_value() {
        let mut env = BTreeMap::new();
        env.insert("NIX_CFLAGS_COMPILE".to_string(), "-I/nix/store/abc ".repeat(100));

        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(), hash_algo: String::new(), hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env,
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    // ── Multiple input sources (sorted output) ──────────

    #[test]
    fn input_sources_sorted_in_serialization() {
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(), hash_algo: String::new(), hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![
                "/nix/store/zzz-last".to_string(),
                "/nix/store/aaa-first".to_string(),
                "/nix/store/mmm-middle".to_string(),
            ],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let serialized = drv.serialize();
        let parsed = Derivation::parse(serialized.as_bytes()).unwrap();
        assert_eq!(parsed.input_sources, vec![
            "/nix/store/aaa-first".to_string(),
            "/nix/store/mmm-middle".to_string(),
            "/nix/store/zzz-last".to_string(),
        ]);
    }

    // ── Error cases ──────────────────────────────────────

    #[test]
    fn parse_truncated_input() {
        assert!(Derivation::parse(b"Derive(").is_err());
        assert!(Derivation::parse(b"").is_err());
        assert!(Derivation::parse(b"Derive").is_err());
    }

    #[test]
    fn parse_invalid_prefix() {
        assert!(Derivation::parse(b"NotDerive([])").is_err());
    }

    #[test]
    fn parse_missing_closing_paren() {
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(), hash_algo: String::new(), hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let mut serialized = drv.serialize();
        serialized.pop(); // Remove the closing ')'
        assert!(Derivation::parse(serialized.as_bytes()).is_err());
    }

    // ── Multiple input derivations with multiple outputs ──

    proptest! {
        #[test]
        fn prop_escape_roundtrip(s in ".*") {
            let escaped = escape(&s);
            let input = format!(
                "Derive([(\"out\",\"/out\",\"\",\"\")],[],[],\"x86_64-linux\",\"/bin/sh\",[],[(\"v\",{escaped})])"
            );
            let drv = Derivation::parse(input.as_bytes()).unwrap();
            prop_assert_eq!(&drv.env["v"], &s);
        }

        #[test]
        fn prop_serialize_parse_roundtrip(
            name in "[a-z]{1,10}",
            builder_arg in "[a-z0-9/]{1,20}",
        ) {
            let drv = Derivation {
                outputs: {
                    let mut m = BTreeMap::new();
                    m.insert("out".to_string(), DerivationOutput {
                        path: format!("/nix/store/hash-{name}"),
                        hash_algo: String::new(),
                        hash: String::new(),
                    });
                    m
                },
                input_derivations: BTreeMap::new(),
                input_sources: vec![],
                system: "x86_64-linux".to_string(),
                builder: format!("/{builder_arg}"),
                args: vec![],
                env: BTreeMap::new(),
            };
            let serialized = drv.serialize();
            let parsed = Derivation::parse(serialized.as_bytes()).unwrap();
            prop_assert_eq!(parsed, drv);
        }
    }

    // ── escape() unit tests ──────────────────────────────

    #[test]
    fn escape_empty_string() {
        assert_eq!(escape(""), "\"\"");
    }

    #[test]
    fn escape_no_special_chars() {
        assert_eq!(escape("hello"), "\"hello\"");
    }

    #[test]
    fn escape_double_quote() {
        assert_eq!(escape("a\"b"), "\"a\\\"b\"");
    }

    #[test]
    fn escape_backslash() {
        assert_eq!(escape("a\\b"), "\"a\\\\b\"");
    }

    #[test]
    fn escape_newline() {
        assert_eq!(escape("a\nb"), "\"a\\nb\"");
    }

    #[test]
    fn escape_carriage_return() {
        assert_eq!(escape("a\rb"), "\"a\\rb\"");
    }

    #[test]
    fn escape_tab() {
        assert_eq!(escape("a\tb"), "\"a\\tb\"");
    }

    #[test]
    fn escape_all_specials_combined() {
        let s = "a\"b\\c\nd\re\tf";
        let escaped = escape(s);
        assert_eq!(escaped, "\"a\\\"b\\\\c\\nd\\re\\tf\"");
    }

    // ── Empty inputs everywhere ──────────────────────────

    #[test]
    fn empty_outputs_btreemap_serializes_brackets() {
        let drv = Derivation {
            outputs: BTreeMap::new(),
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: String::new(),
            builder: String::new(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let s = drv.serialize();
        // outputs is the very first list — must be empty `[]`
        assert!(s.starts_with("Derive([],"));
        let parsed = Derivation::parse(s.as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    #[test]
    fn unicode_env_values_roundtrip() {
        let mut env = BTreeMap::new();
        env.insert("greeting".to_string(), "こんにちは世界".to_string());
        env.insert("emoji".to_string(), "🎉🚀".to_string());
        env.insert("mixed".to_string(), "test 日本語 café".to_string());
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(),
                    hash_algo: String::new(),
                    hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env,
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed, drv);
    }

    #[test]
    fn unknown_escape_sequences_preserved_literally() {
        // The parser preserves unknown \X as \X (literal backslash + char)
        let input = b"Derive([(\"out\",\"/out\",\"\",\"\")],[],[],\"x86_64-linux\",\"/bin/sh\",[],[(\"v\",\"a\\xb\")])";
        let drv = Derivation::parse(input).unwrap();
        // \x is not a recognized escape, so it's preserved as-is
        assert_eq!(drv.env["v"], "a\\xb");
    }

    #[test]
    fn parse_error_includes_position() {
        // Truncated input — parsing fails somewhere
        let result = Derivation::parse(b"Derive([(\"out\"");
        assert!(result.is_err());
    }

    #[test]
    fn parse_invalid_outputs_format() {
        // Outputs missing the closing tuple paren
        let result = Derivation::parse(b"Derive([(\"out\",\"/out\",\"\",\"\"],[],[],\"\",\"\",[],[])");
        assert!(result.is_err());
    }

    #[test]
    fn parse_missing_comma_separator() {
        // Missing comma between fields
        let result = Derivation::parse(b"Derive([],[][],\"\",\"\",[],[])");
        assert!(result.is_err());
    }

    #[test]
    fn args_with_many_entries() {
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(),
                    hash_algo: String::new(),
                    hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: (0..50).map(|i| format!("arg-{i}")).collect(),
            env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.args.len(), 50);
        assert_eq!(parsed, drv);
    }

    #[test]
    fn input_derivations_with_many_outputs_each() {
        let mut input_drvs = BTreeMap::new();
        let outputs: Vec<String> =
            ["out", "dev", "lib", "bin", "doc", "man", "info", "static", "debug"]
                .iter()
                .map(ToString::to_string)
                .collect();
        for i in 0..5 {
            input_drvs.insert(format!("/nix/store/dep{i}.drv"), outputs.clone());
        }
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(),
                    hash_algo: String::new(),
                    hash: String::new(),
                });
                m
            },
            input_derivations: input_drvs,
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.input_derivations.len(), 5);
        for outs in parsed.input_derivations.values() {
            assert_eq!(outs.len(), 9);
        }
        assert_eq!(parsed, drv);
    }

    #[test]
    fn fixed_output_md5_algorithm() {
        let mut outputs = BTreeMap::new();
        outputs.insert("out".to_string(), DerivationOutput {
            path: "/nix/store/abc-x".to_string(),
            hash_algo: "md5".to_string(),
            hash: "0123456789abcdef0123456789abcdef".to_string(),
        });
        let drv = Derivation {
            outputs,
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.outputs["out"].hash_algo, "md5");
        assert_eq!(parsed, drv);
    }

    #[test]
    fn empty_string_in_env_key() {
        let mut env = BTreeMap::new();
        env.insert(String::new(), "value-with-empty-key".to_string());
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(),
                    hash_algo: String::new(),
                    hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "x86_64-linux".to_string(),
            builder: "/bin/sh".to_string(),
            args: vec![],
            env,
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed.env[""], "value-with-empty-key");
    }

    #[test]
    fn parser_handles_zero_outputs_zero_inputs() {
        let input = b"Derive([],[],[],\"sys\",\"build\",[],[])";
        let drv = Derivation::parse(input).unwrap();
        assert!(drv.outputs.is_empty());
        assert!(drv.input_derivations.is_empty());
        assert!(drv.input_sources.is_empty());
        assert!(drv.args.is_empty());
        assert!(drv.env.is_empty());
        assert_eq!(drv.system, "sys");
        assert_eq!(drv.builder, "build");
    }

    #[test]
    fn default_derivation_is_empty() {
        let drv = Derivation::default();
        assert!(drv.outputs.is_empty());
        assert!(drv.input_derivations.is_empty());
        assert!(drv.input_sources.is_empty());
        assert!(drv.system.is_empty());
        assert!(drv.builder.is_empty());
        assert!(drv.args.is_empty());
        assert!(drv.env.is_empty());
    }

    #[test]
    fn default_derivation_output_is_empty() {
        let out = DerivationOutput::default();
        assert!(out.path.is_empty());
        assert!(out.hash_algo.is_empty());
        assert!(out.hash.is_empty());
    }

    #[test]
    fn outputs_serialized_in_btreemap_order() {
        // BTreeMap iterates keys in sorted order — confirm serialization
        // emits outputs alphabetically.
        let mut outputs = BTreeMap::new();
        for name in ["zzz", "aaa", "mmm"] {
            outputs.insert(name.to_string(), DerivationOutput {
                path: format!("/out-{name}"),
                hash_algo: String::new(),
                hash: String::new(),
            });
        }
        let drv = Derivation {
            outputs,
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "sys".to_string(),
            builder: "build".to_string(),
            args: vec![],
            env: BTreeMap::new(),
        };
        let s = drv.serialize();
        // "aaa" should come before "mmm" before "zzz"
        let aaa = s.find("\"aaa\"").unwrap();
        let mmm = s.find("\"mmm\"").unwrap();
        let zzz = s.find("\"zzz\"").unwrap();
        assert!(aaa < mmm);
        assert!(mmm < zzz);
    }

    #[test]
    fn env_serialized_in_btreemap_order() {
        let mut env = BTreeMap::new();
        for k in ["zoo", "alpha", "middle"] {
            env.insert(k.to_string(), format!("v-{k}"));
        }
        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/out".to_string(),
                    hash_algo: String::new(),
                    hash: String::new(),
                });
                m
            },
            input_derivations: BTreeMap::new(),
            input_sources: vec![],
            system: "sys".to_string(),
            builder: "build".to_string(),
            args: vec![],
            env,
        };
        let s = drv.serialize();
        let alpha = s.find("\"alpha\"").unwrap();
        let middle = s.find("\"middle\"").unwrap();
        let zoo = s.find("\"zoo\"").unwrap();
        assert!(alpha < middle);
        assert!(middle < zoo);
    }

    #[test]
    fn complex_input_derivations() {
        let mut input_drvs = BTreeMap::new();
        input_drvs.insert(
            "/nix/store/abc-gcc.drv".to_string(),
            vec!["out".to_string(), "lib".to_string(), "info".to_string()],
        );
        input_drvs.insert(
            "/nix/store/def-glibc.drv".to_string(),
            vec!["out".to_string(), "dev".to_string(), "static".to_string()],
        );
        input_drvs.insert(
            "/nix/store/ghi-bash.drv".to_string(),
            vec!["out".to_string()],
        );

        let drv = Derivation {
            outputs: {
                let mut m = BTreeMap::new();
                m.insert("out".to_string(), DerivationOutput {
                    path: "/nix/store/result".to_string(),
                    hash_algo: String::new(),
                    hash: String::new(),
                });
                m
            },
            input_derivations: input_drvs,
            input_sources: vec!["/nix/store/src".to_string()],
            system: "x86_64-linux".to_string(),
            builder: "/nix/store/bash/bin/bash".to_string(),
            args: vec!["-e".to_string(), "/nix/store/builder.sh".to_string()],
            env: {
                let mut e = BTreeMap::new();
                e.insert("name".to_string(), "test-pkg".to_string());
                e.insert("version".to_string(), "1.0.0".to_string());
                e
            },
        };
        let parsed = Derivation::parse(drv.serialize().as_bytes()).unwrap();
        assert_eq!(parsed, drv);
        assert_eq!(parsed.input_derivations.len(), 3);
        assert_eq!(parsed.input_derivations["/nix/store/abc-gcc.drv"].len(), 3);
    }
}