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
from testlib.fixtures.nix import Nix
from testlib.fixtures.git import Git
from testlib.fixtures.file_helper import with_files, File, EnvTemplate
from testlib.utils import get_global_asset_pack
from pathlib import Path
import pytest
import fnmatch
import json


@pytest.fixture(autouse=True)
def add_xp_features(nix: Nix):
    nix.settings.add_xp_feature("nix-command", "flakes")


@with_files(
    {
        "flakeA": get_global_asset_pack(".git")
        | {
            "flake.nix": EnvTemplate("""{
                inputs = {
                    B = {
                        url = "path:./flakeB";
                        inputs.foobar.follows = "foobar";
                    };
                    foobar.url = "path:@HOME@/flakeA/flakeE";
                };
                outputs = { ... }: {};
            }"""),
            "flakeB": {
                "flake.nix": EnvTemplate("""{
                    inputs = {
                        foobar.url = "path:@HOME@/flakeA/flakeE";
                        goodoo.follows = "C/goodoo";
                        C = {
                            url = "path:./flakeC";
                            inputs.foobar.follows = "foobar";
                        };
                    };
                    outputs = { ... }: {};
                }"""),
                "flakeC": {
                    "flake.nix": EnvTemplate("""{
                        inputs = {
                            foobar.url = "path:@HOME@/flakeA/flakeE";
                            goodoo.follows = "foobar";
                        };
                        outputs = { ... }: {};
                    }""")
                },
            },
            "flakeD": {"flake.nix": File("{ outputs = { ... }: {}; }")},
            "flakeE": {"flake.nix": File("{ outputs = { ... }: {}; }")},
        }
    }
)
class TestPathFollows:
    @pytest.fixture(autouse=True)
    def init(self, git: Git, files: Path):
        git(files / "flakeA", "add", ".")

    def test_metadata(self, nix: Nix, files: Path):
        out = nix.nix(["flake", "metadata", files / "flakeA"]).run().ok()
        assert "B: path" in out.stdout_s
        assert "C: path" in out.stdout_s
        assert "foobar: path" in out.stdout_s
        assert "goodoo follows" in out.stdout_s

    def test_update(self, nix: Nix, files: Path):
        out = nix.nix(["flake", "update", "--flake", files / "flakeA"]).run().ok()
        assert "Added input 'B" in out.stderr_s
        assert "Added input 'B/C" in out.stderr_s
        assert "Added input 'B/C/foobar" in out.stderr_s
        assert "Added input 'B/C/goodoo" in out.stderr_s
        assert "Added input 'B/foobar" in out.stderr_s
        assert "Added input 'B/goodoo" in out.stderr_s

    def test_lock(self, nix: Nix, files: Path):
        nix.nix(["flake", "lock", files / "flakeA"]).run().ok()
        lockfile = json.loads((files / "flakeA/flake.lock").read_text())

        assert lockfile["nodes"]["B"]["inputs"]["C"] == "C"
        assert lockfile["nodes"]["B"]["inputs"]["foobar"] == ["foobar"]
        assert lockfile["nodes"]["C"]["inputs"]["foobar"] == ["B", "foobar"]

        # Ensure that locking twice doesn't change anything
        nix.nix(["flake", "lock", files / "flakeA"]).run().ok()
        lockfile2 = json.loads((files / "flakeA/flake.lock").read_text())
        assert lockfile == lockfile2

    def test_removing_follows_updates_lockfile(self, nix: Nix, files: Path, git: Git):
        git(files / "flakeA", "add", ".")

        nix.nix(["flake", "lock", files / "flakeA"]).run().ok()
        (files / "flakeA/flake.nix").write_text("""{
            inputs = {
                B.url = "path:./flakeB";
                D.url = "path:./flakeD";
            };
            outputs = { ... }: {};
        }""")

        nix.nix(["flake", "lock", files / "flakeA"]).run().ok()

        lockfile = json.loads((files / "flakeA/flake.lock").read_text())
        assert lockfile["nodes"]["B"]["inputs"]["foobar"] == "foobar"
        assert "foobar" in lockfile["nodes"]


@with_files(
    {
        "flakeA": get_global_asset_pack(".git")
        | {
            "flake.nix": File("""{
                inputs.B.url = "path:../flakeB";
                outputs = { ... }: {};
            }""")
        }
    }
)
def test_relative_path_cant_leave_store_path(nix: Nix, files: Path, git: Git):
    git(files / "flakeA", "add", ".")

    out = nix.nix(["flake", "lock", files / "flakeA"]).run().expect(1)
    assert "points outside" in out.stderr_s


@with_files(
    {
        "flakeA": get_global_asset_pack(".git")
        | {
            "flake.nix": File("""{
                inputs.B = {
                    url = "path:./flakeB";
                    inputs.invalid.follows = "D";
                    inputs.invalid2.url = "path:./flakeD";
                };
                inputs.D.url = "path:./flakeD";
                outputs = { ... }: {};
            }"""),
            "flakeB": {
                "flake.nix": EnvTemplate("""{
                    inputs = {
                        foobar.url = "path:@HOME@/flakeA/flakeE";
                        goodoo.follows = "C/goodoo";
                        C = {
                            url = "path:./flakeC";
                            inputs.foobar.follows = "foobar";
                        };
                    };
                    outputs = { ... }: {};
                }"""),
                "flakeC": {
                    "flake.nix": EnvTemplate("""{
                        inputs = {
                            foobar.url = "path:@HOME@/flakeA/flakeE";
                            goodoo.follows = "foobar";
                        };
                        outputs = { ... }: {};
                    }""")
                },
            },
            "flakeD": {"flake.nix": File("{ outputs = { ... }: {}; }")},
            "flakeE": {"flake.nix": File("{ outputs = { ... }: {}; }")},
        }
    }
)
def test_nonexistent_follows_print_warnings(nix: Nix, files: Path, git: Git):
    git(files / "flakeA", "add", ".")

    out = nix.nix(["flake", "lock", files / "flakeA"]).run().ok()
    assert "warning: input 'B' has an override for a non-existent input 'invalid'" in out.stderr_s
    assert "warning: input 'B' has an override for a non-existent input 'invalid2'" in out.stderr_s


@with_files(
    {
        "flakeA": get_global_asset_pack(".git")
        | {
            # input B/D should be able to be found...
            "flake.nix": File("""{
                inputs = {
                    B = {
                        url = "path:./flakeB";
                        inputs.C.follows = "C";
                    };
                    C.url = "path:./flakeB/flakeC";
                };
                outputs = { ... }: {};
            }"""),
            "flakeB": {
                "flake.nix": File("""{
                    inputs = {
                        C.url = "path:./flakeC";
                        D.follows = "C/D";
                    };
                    outputs = { ... }: {};
                }"""),
                "flakeC": {
                    "flake.nix": File("""{
                        inputs.D.url = "path:./flakeD";
                        outputs = { ... }: {};
                    }"""),
                    "flakeD": {"flake.nix": File("{ outputs = { ... }: {}; }")},
                },
            },
        }
    }
)
def test_follow_path_overloading(nix: Nix, files: Path, git: Git):
    r"""
    This tests a lockfile checking regression https://github.com/NixOS/nix/pull/8819

    We construct the following graph, where p->q means p has input q.
    A double edge means that the edge gets overridden using `follows`.

         A
        / \
       /   \
      v     v
      B ==> C   --- follows declared in A
       \\  /
        \\/     --- follows declared in B
         v
         D

    The message was
       error: input 'B/D' follows a non-existent input 'B/C/D'

    Note that for `B` to resolve its follow for `D`, it needs `C/D`,
    for which it needs to resolve the follow on `C` first.
    """

    git(files / "flakeA", "add", ".")

    nix.nix(["flake", "metadata", f"{files}/flakeA"]).run().ok()
    nix.nix(["flake", "update", "--flake", f"{files}/flakeA"]).run().ok()
    nix.nix(["flake", "lock", f"{files}/flakeA"]).run().ok()

    lockfile = json.loads((files / "flakeA/flake.lock").read_text())
    assert lockfile["nodes"]["B"]["inputs"] == {"C": ["C"], "D": ["B", "C", "D"]}
    assert lockfile["nodes"]["C"]["inputs"] == {"D": "D"}
    assert lockfile["nodes"]["root"]["inputs"] == {"B": "B", "C": "C"}


@with_files(
    {
        "flakeA": {
            "flake.nix": EnvTemplate("""{
                inputs.B.url = "path:@HOME@/flakeA/flakeB";
                inputs.D.url = "path:@HOME@/flakeA/flakeD";
                inputs.B.inputs.C.inputs.D.follows = "D";
                outputs = _: {};
            }"""),
            "flakeB": {
                "flake.nix": EnvTemplate("""{
                    inputs.C.url = "path:@HOME@/flakeA/flakeB/flakeC";
                    outputs = _: {};
                }"""),
                "flakeC": {
                    "flake.nix": EnvTemplate("""{
                      inputs.D.url = "path:nosuchflake";
                      outputs = _: {};
                    }""")
                },
            },
            "flakeD": {"flake.nix": File("{ outputs = _: {}; }")},
        }
    }
)
def test_nested_overrides(nix: Nix, files: Path):
    nix.nix(["flake", "lock", f"{files}/flakeA"]).run().ok()
    lockfile = json.loads((files / "flakeA/flake.lock").read_text())
    assert lockfile["nodes"]["C"]["inputs"]["D"] == ["D"]


@with_files(
    {
        "flakeA": {
            "flake.nix": EnvTemplate("""{
                inputs.B.url = "path:@HOME@/flakeA/flakeB";
                inputs.C.url = "path:@HOME@/flakeA/flakeB/flakeC";
                inputs.B.inputs.C.follows = "C";
                outputs = _: {};
            }"""),
            "flakeB": {
                "flake.nix": EnvTemplate("""{
                    inputs.C.url = "path:nosuchflake";
                    inputs.D.url = "path:nosuchflake";
                    inputs.D.follows = "C/D";
                    outputs = _: {};
                }"""),
                "flakeC": {
                    "flake.nix": EnvTemplate("""{
                      inputs.D.url = "path:@HOME@/flakeA/flakeD";
                      outputs = _: {};
                    }""")
                },
            },
            "flakeD": {"flake.nix": File("{ outputs = _: {}; }")},
        }
    }
)
def test_overlapping_flake_follows(nix: Nix, files: Path):
    # bug was not triggered without recreating the lockfile
    nix.nix(["flake", "update", "--flake", f"{files}/flakeA"]).run().ok()
    lockfile = json.loads((files / "flakeA/flake.lock").read_text())
    assert lockfile["nodes"]["B"]["inputs"]["D"] == ["B", "C", "D"]


@with_files(
    {
        "flakeA": {
            "flake.nix": EnvTemplate("""{
                inputs.B.url = "path:@HOME@/flakeA/flakeB";
                inputs.C.url = "path:@HOME@/flakeA/flakeB/flakeC";
                inputs.B.inputs.C.inputs.E.follows = "C";
                outputs = _: {};
            }"""),
            "flakeB": {
                "flake.nix": EnvTemplate("""{
                    inputs.C.url = "path:@HOME@/flakeA/flakeB/flakeC";
                    outputs = _: {};
                }"""),
                "flakeC": {
                    "flake.nix": EnvTemplate("""{
                      inputs.D.url = "path:@HOME@/flakeA/flakeD";
                      outputs = _: {};
                    }""")
                },
            },
            "flakeD": {"flake.nix": File("{ outputs = _: {}; }")},
        }
    }
)
def test_override_nonexistant_input(nix: Nix, files: Path):
    """Test overlapping flake follows: B has D follow C/D, while A has B/C follow C"""
    out = nix.nix(["flake", "update", "--flake", f"{files}/flakeA"]).run().ok()
    assert "warning: input 'B/C' has an override for a non-existent input 'E'" in out.stderr_s


@with_files(
    {
        "flakeA": {
            "flake.nix": EnvTemplate("""{
                inputs = {
                  B.url = "path:@HOME@/flakeA/flakeB";
                  C = {
                    url = "path:@HOME@/flakeA/flakeB/flakeC";
                    inputs.D.inputs.E.follows = "B";
                  };
                };
                outputs = _: {};
            }"""),
            "flakeB": {
                "flake.nix": File("{ outputs = _: {}; }"),
                "flakeC": {
                    "flake.nix": EnvTemplate("""{
                      inputs.D.url = "path:@HOME@/flakeA/flakeD";
                      outputs = _: {};
                    }""")
                },
            },
            "flakeD": {
                "flake.nix": File("""{
                    inputs.E.url = "path:@HOME@/flakeA/flakeE";
                    outputs = _: {};
                }""")
            },
            "flakeE": {"flake.nix": File("{ outputs = { ... }: {}; }")},
        }
    }
)
def test_dep_fetch(nix: Nix, files: Path):
    """Test for Nested follows cause flake interactions to update the nested input #460"""

    # Lockfiles are cleared, initially the dependency needs to be fetched.
    out = nix.nix(["--verbose", "flake", "show", f"path:{files}/flakeA"]).run().ok()
    assert fnmatch.fnmatch(out.stderr_s, "*\nfetching path input 'path:*/flakeD'\n*")

    # But on another flake command it doesn't.
    out = nix.nix(["--verbose", "flake", "show", f"path:{files}/flakeA"]).run().ok()
    assert not fnmatch.fnmatch(out.stderr_s, "*\nfetching path input 'path:*/flakeD'\n*")

    # Make sure the nested override is actually correct in this testcase.
    metadata = nix.nix(["flake", "metadata", f"path:{files}/flakeA", "--json"]).run().json()
    assert metadata["locks"]["nodes"]["D"]["inputs"]["E"] == ["B"]