flake-edit 0.3.6

Edit your flake inputs with ease.
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
//! Validation for Nix flake expressions.
//!
//! - [`error`]: [`ValidationError`], [`Severity`], [`Location`].
//! - `syntax` (private): rnix parse errors and duplicate-attribute detection.
//! - `follows` (crate-private): cycle, stale, target, contradiction, and depth
//!   lints.
//!
//! [`validate`] runs the syntax-level lints. [`validate_full`] adds the
//! follows-graph lints that need a parsed [`InputMap`] and an optional
//! [`FlakeLock`].

pub mod error;
pub(crate) mod follows;
mod syntax;

pub use error::{DuplicateAttr, Location, Severity, ValidationError, ValidationResult};

pub(crate) use syntax::ParsedSource;

use crate::edit::InputMap;
use crate::follows::{DEFAULT_MAX_DEPTH, FollowsGraph};
use crate::lock::{FlakeLock, NestedInput};

/// Run the syntax-level lints over `source`: parse errors, duplicate
/// attributes, and the always-on declared-cycle check.
pub fn validate(source: &str) -> ValidationResult {
    let parsed = ParsedSource::new(source);
    validate_parsed(&parsed)
}

/// [`validate`] for callers that already hold a [`ParsedSource`], so the
/// rnix parse is shared with [`crate::walk::Walker`] construction.
pub(crate) fn validate_parsed(parsed: &ParsedSource) -> ValidationResult {
    let mut errors: Vec<ValidationError> = Vec::new();
    syntax::collect_with_parsed(parsed, &mut errors);
    if errors.is_empty() {
        let mut walker = crate::walk::Walker::from_root(parsed.syntax.clone());
        if walker.walk(&crate::change::Change::None).is_ok() {
            let graph = crate::follows::FollowsGraph::from_declared(&walker.inputs);
            let offset_to_location = |offset: usize| parsed.line_map.offset_to_location(offset);
            errors.extend(follows::lint_follows_cycle(&graph, &offset_to_location));
        }
    }
    ValidationResult {
        errors,
        warnings: Vec::new(),
    }
}

/// Run syntax checks plus every follows-graph lint.
///
/// Walks `flake.lock` once via [`FlakeLock::nested_inputs`], builds the lock
/// graph from that single walk, and hands both to
/// [`validate_full_with_lock_graph`].
pub fn validate_full(
    source: &str,
    inputs: &InputMap,
    lock: Option<&FlakeLock>,
) -> ValidationResult {
    let parsed = ParsedSource::new(source);
    let nested_inputs = lock.map(FlakeLock::nested_inputs);
    let lock_graph = nested_inputs
        .as_deref()
        .map(FollowsGraph::from_nested_inputs);
    validate_full_with_lock_graph(
        &parsed,
        inputs,
        lock_graph.as_ref(),
        nested_inputs.as_deref().unwrap_or(&[]),
    )
}

/// Like [`validate_full`] but skips the lock-drift lints (`lint_follows_stale`
/// and `lint_follows_stale_lock`) that compare declared edges in `flake.nix`
/// against `flake.lock`.
///
/// For speculative validation during a multi-step apply. The lockfile cannot
/// reflect mid-batch text edits, so a freshly-added follows always looks
/// stale relative to the on-disk lock. Running lock-drift lints there would
/// flag every in-progress edit as drift.
pub fn validate_speculative(
    source: &str,
    inputs: &InputMap,
    lock: Option<&FlakeLock>,
) -> ValidationResult {
    let parsed = ParsedSource::new(source);
    let lock_graph = lock.map(FollowsGraph::from_lock);
    validate_speculative_parsed(&parsed, inputs, lock_graph.as_ref())
}

/// [`validate_speculative`] for callers that already hold a [`ParsedSource`]
/// and a pre-built [`FollowsGraph`] of the lockfile (typically from
/// [`FollowsGraph::from_lock`]). Pass `lock_graph = None` to validate against
/// declared edges only.
///
/// Skips the duplicate-attribute lints. The apply loop's
/// [`crate::change::Change`] variants cannot introduce duplicates the source
/// did not already carry: `Add` rejects existing ids, `Follows` mutates in
/// place, `Remove` only deletes. [`validate_full`] runs once before the batch
/// and covers the original duplicate state.
pub(crate) fn validate_speculative_parsed(
    parsed: &ParsedSource,
    inputs: &InputMap,
    lock_graph: Option<&FollowsGraph>,
) -> ValidationResult {
    let mut errors: Vec<ValidationError> = parsed.parse_errors.to_vec();
    let mut warnings: Vec<ValidationError> = Vec::new();
    let graph = follows::build_graph_with_lock_graph(inputs, lock_graph, DEFAULT_MAX_DEPTH);
    run_follows_lints(parsed, inputs, &graph, None, &mut errors, &mut warnings);
    ValidationResult { errors, warnings }
}

/// [`validate_full`] for callers that already hold a [`ParsedSource`], a
/// lockfile-derived [`FollowsGraph`] (from [`FollowsGraph::from_lock`] or
/// [`FollowsGraph::from_nested_inputs`]), and the lockfile's nested-input
/// set. Reuses all three instead of re-walking `flake.lock`.
///
/// `lock_graph = Some(..)` enables the lock-drift lints (`lint_follows_stale`
/// and `lint_follows_stale_lock`), which read `nested_inputs`. `None` skips
/// them and `nested_inputs` is ignored.
pub(crate) fn validate_full_with_lock_graph(
    parsed: &ParsedSource,
    inputs: &InputMap,
    lock_graph: Option<&FollowsGraph>,
    nested_inputs: &[NestedInput],
) -> ValidationResult {
    let mut errors: Vec<ValidationError> = Vec::new();
    let mut warnings: Vec<ValidationError> = Vec::new();
    syntax::collect_with_parsed(parsed, &mut errors);
    let graph = follows::build_graph_with_lock_graph(inputs, lock_graph, DEFAULT_MAX_DEPTH);
    let nested = lock_graph.is_some().then_some(nested_inputs);
    run_follows_lints(parsed, inputs, &graph, nested, &mut errors, &mut warnings);
    ValidationResult { errors, warnings }
}

/// Run every follows-graph lint and route results into `errors`/`warnings` by
/// severity. `nested_inputs` enables the lock-drift lints (stale and
/// stale-lock); pass `None` to skip them.
fn run_follows_lints(
    parsed: &ParsedSource,
    inputs: &InputMap,
    graph: &FollowsGraph,
    nested_inputs: Option<&[NestedInput]>,
    errors: &mut Vec<ValidationError>,
    warnings: &mut Vec<ValidationError>,
) {
    let offset_to_location = |offset: usize| parsed.line_map.offset_to_location(offset);

    let mut candidates: Vec<ValidationError> = Vec::new();
    candidates.extend(follows::lint_follows_cycle(graph, &offset_to_location));
    if let Some(nested) = nested_inputs {
        candidates.extend(follows::lint_follows_stale(graph, &offset_to_location));
        candidates.extend(follows::lint_follows_stale_lock(
            graph,
            nested,
            &offset_to_location,
        ));
    }
    let top_level = follows::top_level_names(inputs);
    candidates.extend(follows::lint_follows_target_not_toplevel(
        graph,
        &top_level,
        &offset_to_location,
    ));
    candidates.extend(follows::lint_follows_contradiction(
        graph,
        &offset_to_location,
    ));
    candidates.extend(follows::lint_follows_depth_exceeded(
        graph,
        DEFAULT_MAX_DEPTH,
        &offset_to_location,
    ));

    for err in candidates {
        match err.severity() {
            Severity::Warning => warnings.push(err),
            Severity::Error => errors.push(err),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::edit::InputMap;
    use crate::follows::{AttrPath, Segment};
    use crate::input::{Follows, Input, Range};
    use crate::validate::error::DuplicateAttr;

    fn expect_duplicate(err: &ValidationError) -> &DuplicateAttr {
        match err {
            ValidationError::DuplicateAttribute(dup) => dup,
            other => panic!("expected DuplicateAttribute, got {other:?}"),
        }
    }

    #[test]
    fn simple_duplicate() {
        let source = "{ a = 1; a = 2; }";
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "a");
        assert_eq!(dup.first.line, 1);
        assert_eq!(dup.first.column, 3);
        assert_eq!(dup.duplicate.line, 1);
        assert_eq!(dup.duplicate.column, 10);
    }

    #[test]
    fn nested_path_duplicate() {
        let source = "{ a.b.c = 1; a.b.c = 2; }";
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "a.b.c");
    }

    #[test]
    fn different_paths_valid() {
        let source = "{ a.b = 1; a.c = 2; }";
        let result = validate(source);
        assert!(result.is_ok());
    }

    #[test]
    fn flake_style_duplicate() {
        let source = r#"{ inputs.nixpkgs.url = "github:nixos/nixpkgs"; inputs.nixpkgs.url = "github:nixos/nixpkgs/unstable"; }"#;
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "inputs.nixpkgs.url");
    }

    #[test]
    fn quoted_attribute_duplicate() {
        let source = r#"{ "a" = 1; a = 2; }"#;
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "a");
    }

    #[test]
    fn nested_attr_set_duplicate() {
        let source = "{ outer = { inner = 1; inner = 2; }; }";
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "inner");
    }

    #[test]
    fn multiple_duplicates() {
        let source = "{ a = 1; a = 2; b = 3; b = 4; }";
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 2);
    }

    #[test]
    fn multiline_flake() {
        let source = r#"{
  inputs.nixpkgs.url = "github:nixos/nixpkgs";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/unstable";
  outputs = { ... }: { };
}"#;
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "inputs.nixpkgs.url");
        assert_eq!(dup.first.line, 2);
        assert_eq!(dup.duplicate.line, 3);
    }

    #[test]
    fn valid_flake() {
        let source = r#"{
  inputs.nixpkgs.url = "github:nixos/nixpkgs";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  outputs = { self, nixpkgs, flake-utils }: { };
}"#;
        let result = validate(source);
        assert!(result.is_ok());
    }

    #[test]
    fn empty_attr_set() {
        let source = "{ }";
        let result = validate(source);
        assert!(result.is_ok());
    }

    #[test]
    fn single_attribute() {
        let source = "{ a = 1; }";
        let result = validate(source);
        assert!(result.is_ok());
    }

    #[test]
    fn parse_error_missing_semicolon() {
        let source = "{ a = 1 }";
        let result = validate(source);
        assert!(result.has_errors());
        assert!(matches!(
            &result.errors[0],
            ValidationError::ParseError { .. }
        ));
    }

    #[test]
    fn parse_error_unclosed_brace() {
        let source = "{ a = 1;";
        let result = validate(source);
        assert!(result.has_errors());
        assert!(matches!(
            &result.errors[0],
            ValidationError::ParseError { .. }
        ));
    }

    #[test]
    fn mergeable_attrsets_valid() {
        let source = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
  };
  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
  };
}"#;
        let result = validate(source);
        assert!(
            result.is_ok(),
            "expected no errors, got: {:?}",
            result.errors
        );
    }

    #[test]
    fn mergeable_attrsets_with_comments() {
        // autofirma-nix pattern: comment-separated input groups
        let source = r#"{
  # Common inputs
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    home-manager.url = "github:nix-community/home-manager";
  };

  # Autofirma sources
  inputs = {
    jmulticard-src = {
      url = "github:ctt-gob-es/jmulticard/v2.0";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, ... }: { };
}"#;
        let result = validate(source);
        assert!(
            result.is_ok(),
            "expected no errors, got: {:?}",
            result.errors
        );
    }

    #[test]
    fn mergeable_attrsets_cross_duplicate() {
        let source = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
  };
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/unstable";
  };
}"#;
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "nixpkgs.url");
    }

    #[test]
    fn non_attrset_duplicate_still_errors() {
        let source = r#"{ a = { x = 1; }; a = 2; }"#;
        let result = validate(source);
        assert!(result.has_errors());
        assert_eq!(result.errors.len(), 1);

        let dup = expect_duplicate(&result.errors[0]);
        assert_eq!(dup.path, "a");
    }

    #[test]
    fn follows_cycle_self_edge_lints() {
        let source = r#"{
  inputs.foo = {
    url = "github:owner/foo";
    inputs.foo.follows = "foo/foo";
  };
  outputs = { ... }: { };
}"#;
        let result = validate(source);
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::FollowsCycle { .. })),
            "expected FollowsCycle, got: {:?}",
            result.errors,
        );
    }

    #[test]
    fn three_mergeable_attrsets() {
        let source = r#"{
  inputs = { a.url = "a"; };
  inputs = { b.url = "b"; };
  inputs = { c.url = "c"; };
}"#;
        let result = validate(source);
        assert!(
            result.is_ok(),
            "expected no errors, got: {:?}",
            result.errors
        );
    }

    fn seg(s: &str) -> Segment {
        Segment::from_unquoted(s).unwrap()
    }

    fn path(s: &str) -> AttrPath {
        AttrPath::parse(s).unwrap()
    }

    fn declared_input(id: &str, follows: &[(&str, &str)]) -> Input {
        let mut input = Input::new(seg(id));
        for (parent, target) in follows {
            input.follows.push(Follows::Indirect {
                path: AttrPath::new(seg(parent)),
                target: Some(path(target)),
            });
        }
        input.range = Range { start: 1, end: 2 };
        input
    }

    fn make_inputs(items: Vec<Input>) -> InputMap {
        let mut map = InputMap::new();
        for input in items {
            map.insert(input.id().as_str().to_string(), input);
        }
        map
    }

    #[test]
    fn validate_full_emits_target_not_toplevel_by_default() {
        let inputs = make_inputs(vec![declared_input("a", &[("b", "missing")])]);
        let result = validate_full("{}", &inputs, None);
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::FollowsTargetNotToplevel { .. })),
            "expected target-not-toplevel error, got: {:?}",
            result.errors,
        );
    }

    #[test]
    fn validate_full_separates_warnings_from_errors() {
        // Stale follows is a warning, target-not-toplevel is an error, and
        // both can fire on the same input.
        let inputs = make_inputs(vec![declared_input("a", &[("b", "missing")])]);
        let lock_text = r#"{
  "nodes": {
    "a": {
      "locked": { "lastModified": 1, "narHash": "", "owner": "o", "repo": "r", "rev": "abc", "type": "github" },
      "original": { "owner": "o", "repo": "r", "type": "github" }
    },
    "root": { "inputs": { "a": "a" } }
  },
  "root": "root",
  "version": 7
}"#;
        let lock = FlakeLock::read_from_str(lock_text).unwrap();
        let result = validate_full("{}", &inputs, Some(&lock));
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::FollowsTargetNotToplevel { .. })),
        );
        assert!(
            result
                .warnings
                .iter()
                .any(|e| matches!(e, ValidationError::FollowsStale { .. })),
            "expected at least one stale warning, got warnings: {:?}",
            result.warnings,
        );
    }
}