]> git.immae.eu Git - perso/Immae/Config/Nix.git/commitdiff
Use relative path for private flakes
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 24 Jan 2021 00:13:31 +0000 (01:13 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 24 Jan 2021 00:13:31 +0000 (01:13 +0100)
flakes/private/openarc/flake.lock
flakes/private/openarc/flake.nix
flakes/private/opendmarc/flake.lock
flakes/private/opendmarc/flake.nix
flakes/private/peertube/flake.lock
flakes/private/peertube/flake.nix
lib/flake-compat-patched.nix [new file with mode: 0644]
lib/flake-compat.nix

index 7bafcaee9e897dfad94944541d34bc062572a559..b1ec2b5f8688e6b134f942a003e55e0d5673e260 100644 (file)
         "openarc": "openarc_2"
       },
       "locked": {
-        "dir": "flakes/openarc",
-        "lastModified": 1611220039,
-        "narHash": "sha256-R6XrtP+YIa+038LmlEo4B3X3RbMzidk/vo5yKTTKFx4=",
-        "ref": "master",
-        "rev": "ef43c36272ca539cbfe803ded03949451b17b679",
-        "revCount": 803,
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "narHash": "sha256-cTe5ak0julFu18fRiXVWZ469km8dPVTRidjl21W+SWc=",
+        "path": "../../openarc",
+        "type": "path"
       },
       "original": {
-        "dir": "flakes/openarc",
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "path": "../../openarc",
+        "type": "path"
       }
     },
     "openarc_2": {
index 6a2518b9e9c2ec6c52fc6c022fc1712870305408..65a56ca161dff276996effd27365f737af967709 100644 (file)
@@ -1,8 +1,7 @@
 {
   inputs.openarc = {
-    url = "https://git.immae.eu/perso/Immae/Config/Nix.git";
-    type = "git";
-    dir = "flakes/openarc";
+    path = "../../openarc";
+    type = "path";
   };
   inputs.nixpkgs.url = "github:NixOS/nixpkgs";
 
index 2c2a07dab2fbfae824cab7e6b0cd302d0c3511a7..ae07161061ef00d44c0377a58ea8adce98b07ea5 100644 (file)
         "nixpkgs": "nixpkgs_3"
       },
       "locked": {
-        "dir": "flakes/opendmarc",
-        "lastModified": 1611220039,
-        "narHash": "sha256-R6XrtP+YIa+038LmlEo4B3X3RbMzidk/vo5yKTTKFx4=",
-        "ref": "master",
-        "rev": "ef43c36272ca539cbfe803ded03949451b17b679",
-        "revCount": 803,
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "narHash": "sha256-V6elpT2t2bYOnY6RSwLIu+SU7Zajkk7oonjscoYpWKo=",
+        "path": "../../opendmarc",
+        "type": "path"
       },
       "original": {
-        "dir": "flakes/opendmarc",
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "path": "../../opendmarc",
+        "type": "path"
       }
     },
     "root": {
index 9aeb3dba9b04e09f9d6ff45424d2f7cfb12f40dc..384bf98cd352ce1783e4a1b65a2ded60160dd7f1 100644 (file)
@@ -1,8 +1,7 @@
 {
   inputs.opendmarc = {
-    url = "https://git.immae.eu/perso/Immae/Config/Nix.git";
-    type = "git";
-    dir = "flakes/opendmarc";
+    path = "../../opendmarc";
+    type = "path";
   };
   inputs.nixpkgs.url = "github:NixOS/nixpkgs";
 
index 3085b0ab6a48988adf0db6be3822dfc58773043a..97c5c6378eaeafb2aa28a72aa5d48ca7c931685e 100644 (file)
         "peertube": "peertube"
       },
       "locked": {
-        "dir": "flakes/peertube",
-        "lastModified": 1611273496,
-        "narHash": "sha256-U+q6Ir9VsL2egLjeXZ6YpRZxwLotKcrp2I0fOX6es+c=",
-        "ref": "master",
-        "rev": "ce95026934c4ea8c647365f68eb195459fcdff08",
-        "revCount": 806,
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "narHash": "sha256-YvUqraTpt/I4jw0mSrJgptLymZwlBvhwSK9VLUKD/cc=",
+        "path": "../../peertube",
+        "type": "path"
       },
       "original": {
-        "dir": "flakes/peertube",
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "path": "../../peertube",
+        "type": "path"
       }
     },
     "peertube_syden": {
         "peertube": "peertube_2"
       },
       "locked": {
-        "dir": "flakes/peertube",
-        "lastModified": 1611270106,
-        "narHash": "sha256-VKDczZDM9A2dGoSAEN6rUDxvLKYUuoDMefQc7fabvXo=",
-        "ref": "master",
-        "rev": "1279eb87299a44167c4ed4a10a9088d58654b5d3",
-        "revCount": 805,
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "narHash": "sha256-YvUqraTpt/I4jw0mSrJgptLymZwlBvhwSK9VLUKD/cc=",
+        "path": "../../peertube",
+        "type": "path"
       },
       "original": {
-        "dir": "flakes/peertube",
-        "type": "git",
-        "url": "https://git.immae.eu/perso/Immae/Config/Nix.git"
+        "path": "../../peertube",
+        "type": "path"
       }
     },
     "root": {
index bb399df031315a855a7f7953766d58d1f40a0c89..e16cbafd38fb9414028f4c1e264322e84a09e29b 100644 (file)
@@ -1,14 +1,12 @@
 {
   description = "Patched peertube";
   inputs.peertube_origin = {
-    url = "https://git.immae.eu/perso/Immae/Config/Nix.git";
-    type = "git";
-    dir = "flakes/peertube";
+    path = "../../peertube";
+    type = "path";
   };
   inputs.peertube_syden = {
-    url = "https://git.immae.eu/perso/Immae/Config/Nix.git";
-    type = "git";
-    dir = "flakes/peertube";
+    path = "../../peertube";
+    type = "path";
     inputs.peertube = {
       url = "https://git.immae.eu/github/Chocobozzz/PeerTube.git";
       ref = "gitolite_local/syden";
diff --git a/lib/flake-compat-patched.nix b/lib/flake-compat-patched.nix
new file mode 100644 (file)
index 0000000..217a99f
--- /dev/null
@@ -0,0 +1,190 @@
+# Compatibility function to allow flakes to be used by
+# non-flake-enabled Nix versions. Given a source tree containing a
+# 'flake.nix' and 'flake.lock' file, it fetches the flake inputs and
+# calls the flake's 'outputs' function. It then returns an attrset
+# containing 'defaultNix' (to be used in 'default.nix'), 'shellNix'
+# (to be used in 'shell.nix').
+
+{ src, system ? builtins.currentSystem or "unknown-system" }:
+
+let
+
+  lockFilePath = src + "/flake.lock";
+
+  lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
+
+  fetchTree =
+    info:
+    if info.type == "github" then
+      { outPath = fetchTarball "https://api.${info.host or "github.com"}/repos/${info.owner}/${info.repo}/tarball/${info.rev}";
+        rev = info.rev;
+        shortRev = builtins.substring 0 7 info.rev;
+        lastModified = info.lastModified;
+        lastModifiedDate = formatSecondsSinceEpoch info.lastModified;
+        narHash = info.narHash;
+      }
+    else if info.type == "git" then
+      { outPath =
+          builtins.fetchGit
+            ({ url = info.url; }
+             // (if info ? rev then { inherit (info) rev; } else {})
+             // (if info ? ref then { inherit (info) ref; } else {})
+            );
+        lastModified = info.lastModified;
+        lastModifiedDate = formatSecondsSinceEpoch info.lastModified;
+        narHash = info.narHash;
+      } // (if info ? rev then {
+        rev = info.rev;
+        shortRev = builtins.substring 0 7 info.rev;
+      } else {
+      })
+    else if info.type == "path" then
+      { outPath = builtins.path { path =
+          if builtins.substring 0 1 info.path == "."
+          then builtins.toString src + "/" + info.path
+          else info.path;
+        };
+        narHash = info.narHash;
+      }
+    else if info.type == "tarball" then
+      { outPath = fetchTarball info.url;
+        narHash = info.narHash;
+      }
+    else if info.type == "gitlab" then
+      { inherit (info) rev narHash lastModified;
+        outPath = fetchTarball "https://${info.host or "gitlab.com"}/api/v4/projects/${info.owner}%2F${info.repo}/repository/archive.tar.gz?sha=${info.rev}";
+        shortRev = builtins.substring 0 7 info.rev;
+      }
+    else
+      # FIXME: add Mercurial, tarball inputs.
+      throw "flake input has unsupported input type '${info.type}'";
+
+  callFlake4 = flakeSrc: locks:
+    let
+      flake = import (flakeSrc + "/flake.nix");
+
+      inputs = builtins.mapAttrs (n: v:
+        if v.flake or true
+        then callFlake4 (fetchTree (v.locked // v.info)) v.inputs
+        else fetchTree (v.locked // v.info)) locks;
+
+      outputs = flakeSrc // (flake.outputs (inputs // {self = outputs;}));
+    in
+      assert flake.edition == 201909;
+      outputs;
+
+  callLocklessFlake = flakeSrc:
+    let
+      flake = import (flakeSrc + "/flake.nix");
+      outputs = flakeSrc // (flake.outputs ({ self = outputs; }));
+    in outputs;
+
+  rootSrc = let
+    # Try to clean the source tree by using fetchGit, if this source
+    # tree is a valid git repository.
+    tryFetchGit = src:
+      if isGit && !isShallow
+      then
+        let res = builtins.fetchGit src;
+        in if res.rev == "0000000000000000000000000000000000000000" then removeAttrs res ["rev" "shortRev"]  else res
+      else { outPath = src; };
+    # NB git worktrees have a file for .git, so we don't check the type of .git
+    isGit = builtins.pathExists (src + "/.git");
+    isShallow = builtins.pathExists (src + "/.git/shallow");
+
+  in
+    { lastModified = 0; lastModifiedDate = formatSecondsSinceEpoch 0; }
+      // (if src ? outPath then src else tryFetchGit src);
+
+  # Format number of seconds in the Unix epoch as %Y%m%d%H%M%S.
+  formatSecondsSinceEpoch = t:
+    let
+      rem = x: y: x - x / y * y;
+      days = t / 86400;
+      secondsInDay = rem t 86400;
+      hours = secondsInDay / 3600;
+      minutes = (rem secondsInDay 3600) / 60;
+      seconds = rem t 60;
+
+      # Courtesy of https://stackoverflow.com/a/32158604.
+      z = days + 719468;
+      era = (if z >= 0 then z else z - 146096) / 146097;
+      doe = z - era * 146097;
+      yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
+      y = yoe + era * 400;
+      doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
+      mp = (5 * doy + 2) / 153;
+      d = doy - (153 * mp + 2) / 5 + 1;
+      m = mp + (if mp < 10 then 3 else -9);
+      y' = y + (if m <= 2 then 1 else 0);
+
+      pad = s: if builtins.stringLength s < 2 then "0" + s else s;
+    in "${toString y'}${pad (toString m)}${pad (toString d)}${pad (toString hours)}${pad (toString minutes)}${pad (toString seconds)}";
+
+  allNodes =
+    builtins.mapAttrs
+      (key: node:
+        let
+          sourceInfo =
+            if key == lockFile.root
+            then rootSrc
+            else fetchTree (node.info or {} // removeAttrs node.locked ["dir"]);
+
+          subdir = if key == lockFile.root then "" else node.locked.dir or "";
+
+          flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix");
+
+          inputs = builtins.mapAttrs
+            (inputName: inputSpec: allNodes.${resolveInput inputSpec})
+            (node.inputs or {});
+
+          # Resolve a input spec into a node name. An input spec is
+          # either a node name, or a 'follows' path from the root
+          # node.
+          resolveInput = inputSpec:
+              if builtins.isList inputSpec
+              then getInputByPath lockFile.root inputSpec
+              else inputSpec;
+
+          # Follow an input path (e.g. ["dwarffs" "nixpkgs"]) from the
+          # root node, returning the final node.
+          getInputByPath = nodeName: path:
+            if path == []
+            then nodeName
+            else
+              getInputByPath
+                # Since this could be a 'follows' input, call resolveInput.
+                (resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path})
+                (builtins.tail path);
+
+          outputs = flake.outputs (inputs // { self = result; });
+
+          result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; };
+        in
+          if node.flake or true then
+            assert builtins.isFunction flake.outputs;
+            result
+          else
+            sourceInfo
+      )
+      lockFile.nodes;
+
+  result =
+    if !(builtins.pathExists lockFilePath)
+    then callLocklessFlake rootSrc
+    else if lockFile.version == 4
+    then callFlake4 rootSrc (lockFile.inputs)
+    else if lockFile.version >= 5 && lockFile.version <= 7
+    then allNodes.${lockFile.root}
+    else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}";
+
+in
+  rec {
+    defaultNix =
+      result
+      // (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {});
+
+    shellNix =
+      defaultNix
+      // (if result ? devShell.${system} then { default = result.devShell.${system}; } else {});
+  }
index 1f0d4929132da2f38fd3061b816d938e90822c10..d3daa10249dfd546226053d84df9f5060fa399f8 100644 (file)
@@ -1,6 +1,8 @@
 src:
-(import (
-  fetchTarball {
-    url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
-    sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2";
-  }) { inherit src; }).defaultNix
+(import ./flake-compat-patched.nix { inherit src; }).defaultNix
+# Until https://github.com/edolstra/flake-compat/pull/18 is accepted
+# (import (
+#   fetchTarball {
+#     url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
+#     sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2";
+#   }) { inherit src; }).defaultNix