flake-edit 0.4.0

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
//! Rendering for `follow --stats` and `--dry-run` summaries.

use std::collections::{BTreeMap, HashMap};

use crate::follows::AttrPath;
use crate::lock::NestedInput;
use crate::lock::sizes::InputSizes;

use super::AppliedPlan;

/// The summary of what this run changed (or would change).
pub(super) fn applied_summary(
    applied: &AppliedPlan,
    wrote: bool,
    stats: bool,
    nested_inputs: &[NestedInput],
    sizes: &InputSizes,
) -> String {
    let mut out = String::new();

    let lock_routes: HashMap<&AttrPath, &AttrPath> = if stats {
        nested_inputs
            .iter()
            .filter_map(|nested| nested.follows.as_ref().map(|target| (&nested.path, target)))
            .collect()
    } else {
        HashMap::new()
    };
    let realized: Vec<&(AttrPath, AttrPath)> = applied
        .applied_follows
        .iter()
        .filter(|(path, target)| lock_routes.get(path) != Some(&target))
        .collect();

    if !realized.is_empty() {
        let verb = if wrote {
            "Deduplicated"
        } else {
            "Would deduplicate"
        };
        let noun = if realized.len() == 1 {
            "input"
        } else {
            "inputs"
        };
        let entry_sizes: Vec<Option<u64>> = realized
            .iter()
            .map(|(path, _)| if stats { sizes.size_for(path) } else { None })
            .collect();
        let suffix = if stats {
            sum_suffix(&entry_sizes, false)
        } else {
            String::new()
        };
        out.push_str(&format!("{verb} {} {noun}{suffix}.\n", realized.len()));
        for ((path, target), size) in realized.iter().zip(&entry_sizes) {
            out.push_str(&format!(
                "  {path} -> {target}{}\n",
                entry_suffix(*size, stats)
            ));
        }
    }

    if !applied.unfollowed.is_empty() {
        let verb = if wrote { "Removed" } else { "Would remove" };
        let noun = if applied.unfollowed.len() == 1 {
            "declaration"
        } else {
            "declarations"
        };
        out.push_str(&format!(
            "{verb} {} stale follows {noun}.\n",
            applied.unfollowed.len()
        ));
        for path in &applied.unfollowed {
            out.push_str(&format!("  {path} (input no longer exists)\n"));
        }
    }

    out
}

/// The summary of follows already existing in `flake.lock`.
/// Grouped by the store path they resolve to.
pub(super) fn existing_summary(
    nested_inputs: &[NestedInput],
    sizes: &InputSizes,
) -> Option<String> {
    let entries: Vec<(&AttrPath, &AttrPath)> = nested_inputs
        .iter()
        .filter_map(|nested| nested.follows.as_ref().map(|target| (&nested.path, target)))
        .collect();
    if entries.is_empty() {
        return None;
    }

    let mut groups: BTreeMap<String, Group> = BTreeMap::new();
    for (path, target) in &entries {
        let store_path = sizes.store_path_for(path);
        let key = store_path.map_or_else(|| format!("target:{target}"), str::to_string);
        let label = store_path
            .and_then(|sp| sizes.label_for_store_path(sp))
            .map_or_else(|| target.to_string(), str::to_string);
        let group = groups.entry(key).or_insert_with(|| Group {
            label,
            count: 0,
            unit_size: sizes.size_for(path),
        });
        group.count += 1;
    }

    let group_totals: Vec<Option<u64>> = groups.values().map(Group::total).collect();
    let count = entries.len();
    let input_noun = if count == 1 { "input" } else { "inputs" };
    let target_noun = if groups.len() == 1 {
        "target"
    } else {
        "targets"
    };

    let mut out = format!(
        "Already deduplicated by existing follows: {count} {input_noun} across {} {target_noun}{}.\n",
        groups.len(),
        sum_suffix(&group_totals, true),
    );

    // Biggest savings first, then by label.
    let mut rows: Vec<&Group> = groups.values().collect();
    rows.sort_by(|a, b| {
        b.total()
            .cmp(&a.total())
            .then_with(|| a.label.cmp(&b.label))
    });
    for group in rows {
        let unit = match group.unit_size {
            Some(bytes) => format!("~{}", format_size(bytes)),
            None => "size unknown".to_string(),
        };
        if group.count == 1 {
            out.push_str(&format!("  {} ({unit})\n", group.label));
        } else {
            let total = group
                .total()
                .map(|bytes| format!(" (~{})", format_size(bytes)))
                .unwrap_or_default();
            out.push_str(&format!(
                "  {} ({unit}) x {}{total}\n",
                group.label, group.count
            ));
        }
    }

    Some(out)
}

/// One row of [`existing_summary`].
struct Group {
    label: String,
    count: u64,
    unit_size: Option<u64>,
}

impl Group {
    fn total(&self) -> Option<u64> {
        self.unit_size.map(|size| size.saturating_mul(self.count))
    }
}

fn sum_suffix(entry_sizes: &[Option<u64>], hypothetical: bool) -> String {
    let known: Vec<u64> = entry_sizes.iter().flatten().copied().collect();
    if known.is_empty() {
        return " (size unknown)".to_string();
    }
    let total = known
        .iter()
        .fold(0u64, |acc, size| acc.saturating_add(*size));
    let qualifier = if known.len() == entry_sizes.len() {
        ""
    } else {
        "at least "
    };
    let size = format_size(total);
    if hypothetical {
        format!(" ({qualifier}~{size})")
    } else {
        format!(" ({qualifier}{size} saved)")
    }
}

fn entry_suffix(size: Option<u64>, stats: bool) -> String {
    if !stats {
        return String::new();
    }
    match size {
        Some(bytes) => format!(" ({})", format_size(bytes)),
        None => " (size unknown)".to_string(),
    }
}

/// Human-readable sizes.
fn format_size(bytes: u64) -> String {
    const KIB: u64 = 1024;
    const MIB: u64 = 1024 * KIB;
    const GIB: u64 = 1024 * MIB;
    if bytes >= GIB {
        format!("{:.1} GiB", bytes as f64 / GIB as f64)
    } else if bytes >= MIB {
        format!("{} MiB", bytes / MIB)
    } else if bytes >= KIB {
        format!("{} KiB", bytes / KIB)
    } else {
        format!("{bytes} B")
    }
}

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

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

    fn nested(path: &str, follows: Option<&str>) -> NestedInput {
        NestedInput {
            path: ap(path),
            follows: follows.map(ap),
            url: None,
        }
    }

    fn applied(follows: &[(&str, &str)], unfollowed: &[&str]) -> AppliedPlan {
        AppliedPlan {
            applied_follows: follows.iter().map(|(s, t)| (ap(s), ap(t))).collect(),
            unfollowed: unfollowed.iter().map(|s| ap(s)).collect(),
            ..AppliedPlan::default()
        }
    }

    #[test]
    fn format_size_picks_unit_by_scale() {
        assert_eq!(format_size(0), "0 B");
        assert_eq!(format_size(2048), "2 KiB");
        assert_eq!(format_size(150 * 1024 * 1024), "150 MiB");
        assert_eq!(
            format_size(1024 * 1024 * 1024 + 512 * 1024 * 1024),
            "1.5 GiB"
        );
    }

    #[test]
    fn sum_suffix_reports_partial_and_unknown_sums() {
        assert_eq!(sum_suffix(&[None, None], false), " (size unknown)");
        assert_eq!(sum_suffix(&[None, None], true), " (size unknown)");
        assert_eq!(
            sum_suffix(&[Some(100 * 1024 * 1024), Some(50 * 1024 * 1024)], false),
            " (150 MiB saved)",
        );
        assert_eq!(
            sum_suffix(&[Some(100 * 1024 * 1024), Some(50 * 1024 * 1024)], true),
            " (~150 MiB)",
        );
        assert_eq!(
            sum_suffix(&[Some(100 * 1024 * 1024), None], false),
            " (at least 100 MiB saved)",
        );
        assert_eq!(
            sum_suffix(&[Some(100 * 1024 * 1024), None], true),
            " (at least ~100 MiB)",
        );
    }

    #[test]
    fn applied_summary_without_stats_renders_plain_summary() {
        let plan = applied(
            &[("home-manager.nixpkgs", "nixpkgs")],
            &["crane.flake-compat"],
        );
        let out = applied_summary(&plan, true, false, &[], &InputSizes::default());
        assert_eq!(
            out,
            "Deduplicated 1 input.\n\
             \x20 home-manager.nixpkgs -> nixpkgs\n\
             Removed 1 stale follows declaration.\n\
             \x20 crane.flake-compat (input no longer exists)\n",
        );
    }

    #[test]
    fn applied_summary_uses_preview_framing_when_not_written() {
        let plan = applied(&[("home-manager.nixpkgs", "nixpkgs")], &["crane.old"]);
        let out = applied_summary(&plan, false, true, &[], &InputSizes::default());
        assert_eq!(
            out,
            "Would deduplicate 1 input (size unknown).\n\
             \x20 home-manager.nixpkgs -> nixpkgs (size unknown)\n\
             Would remove 1 stale follows declaration.\n\
             \x20 crane.old (input no longer exists)\n",
        );
    }

    #[test]
    fn applied_summary_excludes_lock_routed_entries_from_realized() {
        // `crane.nixpkgs` is already deduplicated,
        // application must not count toward realized savings.
        let plan = applied(
            &[
                ("home-manager.nixpkgs", "nixpkgs"),
                ("crane.nixpkgs", "nixpkgs"),
            ],
            &[],
        );
        let nested_inputs = [nested("crane.nixpkgs", Some("nixpkgs"))];
        let sizes = InputSizes::from_attr_entries([
            (
                "home-manager.nixpkgs".to_string(),
                "/store/a".to_string(),
                Some(100 * 1024 * 1024),
            ),
            (
                "crane.nixpkgs".to_string(),
                "/store/a".to_string(),
                Some(100 * 1024 * 1024),
            ),
        ]);
        let out = applied_summary(&plan, true, true, &nested_inputs, &sizes);
        assert_eq!(
            out,
            "Deduplicated 1 input (100 MiB saved).\n\
             \x20 home-manager.nixpkgs -> nixpkgs (100 MiB)\n",
        );
    }

    #[test]
    fn applied_summary_keeps_retargeted_entries_in_realized() {
        let plan = applied(&[("crane.nixpkgs", "nixpkgs")], &[]);
        let nested_inputs = [nested("crane.nixpkgs", Some("flake-utils.nixpkgs"))];
        let out = applied_summary(&plan, true, true, &nested_inputs, &InputSizes::default());
        assert_eq!(
            out,
            "Deduplicated 1 input (size unknown).\n\
             \x20 crane.nixpkgs -> nixpkgs (size unknown)\n",
        );
    }

    #[test]
    fn existing_summary_saturates_absurd_sizes() {
        let nested_inputs = [
            nested("a.nixpkgs", Some("nixpkgs")),
            nested("b.nixpkgs", Some("nixpkgs")),
        ];
        let sizes = InputSizes::from_attr_entries([
            (
                "a.nixpkgs".to_string(),
                "/store/a".to_string(),
                Some(u64::MAX),
            ),
            ("b.nixpkgs".to_string(), "/store/a".to_string(), None),
        ]);
        let out = existing_summary(&nested_inputs, &sizes).expect("has follows");
        assert_eq!(
            out,
            "Already deduplicated by existing follows: 2 inputs across 1 target \
             (~17179869184.0 GiB).\n\
             \x20 a.nixpkgs (~17179869184.0 GiB) x 2 (~17179869184.0 GiB)\n",
        );
    }

    #[test]
    fn existing_summary_groups_followers_by_store_path() {
        let nested_inputs = [
            nested("a.nixpkgs", Some("nixpkgs")),
            nested("b.nixpkgs", Some("nixpkgs")),
            nested("a.flake-utils", Some("flake-utils")),
        ];
        let mib = 1024 * 1024;
        let sizes = InputSizes::from_attr_entries([
            (
                "nixpkgs".to_string(),
                "/store/a".to_string(),
                Some(200 * mib),
            ),
            ("a.nixpkgs".to_string(), "/store/a".to_string(), None),
            ("b.nixpkgs".to_string(), "/store/a".to_string(), None),
            (
                "a.flake-utils".to_string(),
                "/store/b".to_string(),
                Some(10 * mib),
            ),
        ]);
        let out = existing_summary(&nested_inputs, &sizes).expect("has follows");
        assert_eq!(
            out,
            "Already deduplicated by existing follows: 3 inputs across 2 targets (~410 MiB).\n\
             \x20 nixpkgs (~200 MiB) x 2 (~400 MiB)\n\
             \x20 a.flake-utils (~10 MiB)\n",
        );
    }

    #[test]
    fn existing_summary_keeps_unknown_targets_apart() {
        // Two unresolved followers of different targets must not merge into one row.
        let nested_inputs = [
            nested("a.nixpkgs", Some("nixpkgs")),
            nested("a.flake-utils", Some("flake-utils")),
        ];
        let out = existing_summary(&nested_inputs, &InputSizes::default()).expect("has follows");
        assert_eq!(
            out,
            "Already deduplicated by existing follows: 2 inputs across 2 targets \
             (size unknown).\n\
             \x20 flake-utils (size unknown)\n\
             \x20 nixpkgs (size unknown)\n",
        );
    }

    #[test]
    fn existing_summary_is_none_without_follows() {
        let nested_inputs = [nested("a.nixpkgs", None)];
        assert!(existing_summary(&nested_inputs, &InputSizes::default()).is_none());
    }
}