Repository: fzakaria/nix-auto-follow

Issue:
Check crashing
Note this is on macOS if that matters. I attach my 
[flake.lock.txt](https://github.com/user-attachments/files/16479126/flake.lock.txt)


`nix run github:fzakaria/nix-auto-follow -- -c`

gives
```
Node cachix has input nixpkgs pointing to nixpkgs_2 which is not the same as crate2nix's nixpkgs which is nixpkgs_3 in the lockfile.
Traceback (most recent call last):
  File "/nix/store/9aqpdql9cwmsvmdd4m33cnxl9nw6z6xc-nix-auto-follow/bin/.auto-follow-wrapped", line 9, in <module>
    sys.exit(start())
             ^^^^^^^
  File "/nix/store/9aqpdql9cwmsvmdd4m33cnxl9nw6z6xc-nix-auto-follow/lib/python3.12/site-packages/nix_auto_follow/cli.py", line 189, in start
    if not check_lock_file(LockFile.from_dict(flake_lock_json)):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/9aqpdql9cwmsvmdd4m33cnxl9nw6z6xc-nix-auto-follow/lib/python3.12/site-packages/nix_auto_follow/cli.py", line 101, in check_lock_file
    f"Please add '{key}.url = \"{flake_lock.nodes[ref].get_url()}\"' or '{other_key}.url = \"{flake_lock.nodes[other_ref].get_url()}'"  # noqa: E501
                                                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/9aqpdql9cwmsvmdd4m33cnxl9nw6z6xc-nix-auto-follow/lib/python3.12/site-packages/nix_auto_follow/cli.py", line 45, in get_url
    raise ValueError(f"Unknown type {original['type']}")
ValueError: Unknown type indirect
```

Discussion:

Patch:
diff --git a/derivation.nix b/derivation.nix
index faf2089..39e6c0c 100644
--- a/derivation.nix
+++ b/derivation.nix
@@ -32,6 +32,11 @@ in
 
     nativeCheckInputs = with python3Packages;
       [pytestCheckHook flake8 mypy isort black]
+      ++ [
+        # FIXME: we are still types-pygments
+        types-colorama
+        types-setuptools
+      ]
       ++ [pyright];
 
     checkPhase = ''
diff --git a/flake.lock b/flake.lock
index 0742afc..44a9275 100644
--- a/flake.lock
+++ b/flake.lock
@@ -2,16 +2,16 @@
   "nodes": {
     "nixpkgs": {
       "locked": {
-        "lastModified": 1722415718,
-        "narHash": "sha256-5US0/pgxbMksF92k1+eOa8arJTJiPvsdZj9Dl+vJkM4=",
+        "lastModified": 1722519197,
+        "narHash": "sha256-VEdJmVU2eLFtLqCjTYJd1J7+Go8idAcZoT11IewFiRg=",
         "owner": "NixOS",
         "repo": "nixpkgs",
-        "rev": "c3392ad349a5227f4a3464dce87bcc5046692fce",
+        "rev": "05405724efa137a0b899cce5ab4dde463b4fd30b",
         "type": "github"
       },
       "original": {
         "owner": "NixOS",
-        "ref": "nixpkgs-unstable",
+        "ref": "nixos-24.05",
         "repo": "nixpkgs",
         "type": "github"
       }
diff --git a/flake.nix b/flake.nix
index d4d01e3..36afe08 100644
--- a/flake.nix
+++ b/flake.nix
@@ -1,6 +1,6 @@
 {
   description = "Achieve nirvana through automatically following all flake inputs.";
-  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
 
   outputs = {
     self,
diff --git a/src/nix_auto_follow/cli.py b/src/nix_auto_follow/cli.py
index cf96e84..090aca9 100644
--- a/src/nix_auto_follow/cli.py
+++ b/src/nix_auto_follow/cli.py
@@ -27,20 +27,27 @@ class Node:
         return {"inputs": self.inputs, **self.remaining}
 
     def get_url(self) -> str:
+        """
+        Should reconstruct the Flake URL for the given Node.
+
+        @see https://nix.dev/manual/nix/2.22/command-ref/new-cli/nix3-flake#types
+        """
         if "original" not in self.remaining:
             raise ValueError("Node does not have a locked attribute.")
         original = self.remaining["original"]
         ref = f"/{original['ref']}" if "ref" in original else ""
+        rev = f"/{original['rev']}" if "rev" in original else ""
+        rev_or_ref = next((x for x in [rev, ref] if x), "")
 
         match original["type"]:
             case "github":
-                return f"github:{original['owner']}/{original['repo']}{ref}"
+                return f"github:{original['owner']}/{original['repo']}{rev_or_ref}"
             case "gitlab":
-                return f"gitlab:{original['owner']}/{original['repo']}{ref}"
-            case "bitbucket":
-                return f"bitbucket:{original['owner']}/{original['repo']}{ref}"
+                return f"gitlab:{original['owner']}/{original['repo']}{rev_or_ref}"
             case "path":
                 return f"file:{original['path']}"
+            case "indirect":
+                return f"{original['id']}{ref}{rev}"
             case _:
                 raise ValueError(f"Unknown type {original['type']}")
 


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index b519b80..a2a40ef 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -12,24 +12,65 @@ from nix_auto_follow.cli import (
 )
 
 
-def test_get_url_for_node() -> None:
-    node = Node.from_dict(
-        {
-            "original": {
-                "owner": "nixos",
-                "ref": "nixos-24.05",
-                "repo": "nixpkgs",
-                "type": "github",
-            }
-        }
-    )
-    assert node.get_url() == "github:nixos/nixpkgs/nixos-24.05"
-
-    # without ref
-    node = Node.from_dict(
-        {"original": {"owner": "nixos", "repo": "nixpkgs", "type": "github"}}
-    )
-    assert node.get_url() == "github:nixos/nixpkgs"
+@pytest.mark.parametrize(
+    "node, expected_url",
+    [
+        (
+            Node.from_dict(
+                {
+                    "original": {
+                        "owner": "nixos",
+                        "ref": "nixos-24.05",
+                        "repo": "nixpkgs",
+                        "type": "github",
+                    }
+                }
+            ),
+            "github:nixos/nixpkgs/nixos-24.05",
+        ),
+        (
+            Node.from_dict(
+                {"original": {"owner": "nixos", "repo": "nixpkgs", "type": "github"}}
+            ),
+            "github:nixos/nixpkgs",
+        ),
+        (
+            Node.from_dict({"original": {"id": "nixpkgs", "type": "indirect"}}),
+            "nixpkgs",
+        ),
+        (
+            Node.from_dict({"original": {"id": "nixpkgs", "type": "indirect"}}),
+            "nixpkgs",
+        ),
+        (
+            Node.from_dict(
+                {
+                    "original": {
+                        "id": "nixpkgs",
+                        "ref": "nixos-unstable",
+                        "type": "indirect",
+                    }
+                }
+            ),
+            "nixpkgs/nixos-unstable",
+        ),
+        (
+            Node.from_dict(
+                {
+                    "original": {
+                        "id": "nixpkgs",
+                        "ref": "nixos-unstable",
+                        "rev": "23.11",
+                        "type": "indirect",
+                    }
+                }
+            ),
+            "nixpkgs/nixos-unstable/23.11",
+        ),
+    ],
+)
+def test_get_url_for_node(node: Node, expected_url: str) -> None:
+    assert node.get_url() == expected_url
 
 
 def test_simple_follow_flake() -> None: