From bc0a7b49406373f05e51196a923fe16abf4066ec Mon Sep 17 00:00:00 2001 From: Eric Rodrigues Pires Date: Sun, 2 Aug 2026 21:29:40 -0300 Subject: [PATCH] nix: update inputs to tack --- .tack/default.nix | 331 +++++++++++++++++++++++++++++++++++++++++++ .tack/pins.lock.json | 26 ++++ .tack/pins.toml | 11 ++ flake.nix | 23 ++- nix/default.nix | 10 +- npins/default.nix | 249 -------------------------------- npins/sources.json | 37 ----- 7 files changed, 391 insertions(+), 296 deletions(-) create mode 100644 .tack/default.nix create mode 100644 .tack/pins.lock.json create mode 100644 .tack/pins.toml delete mode 100644 npins/default.nix delete mode 100644 npins/sources.json diff --git a/.tack/default.nix b/.tack/default.nix new file mode 100644 index 0000000..71a5407 --- /dev/null +++ b/.tack/default.nix @@ -0,0 +1,331 @@ +# SPDX-License-Identifier: EUPL-1.2 +# tack-managed resolver. delete this line to take ownership; tack will leave it alone afterwards. + +let + inherit (builtins) + attrNames + attrValues + concatMap + elem + elemAt + filter + foldl' + fromJSON + head + intersectAttrs + isList + isString + listToAttrs + mapAttrs + match + pathExists + readFile + substring + tail + trace + ; + + call = + { + overrides ? { }, + }: + let + pins = fromTOML (readFile ./pins.toml); + lock = fromJSON (readFile ./pins.lock.json); + all_follow_raw = pins.all_follow or { }; + + # flatten `target = [aliases]` rows alongside `alias = "target"` rows + all_follow = foldl' ( + acc: key: + let + val = all_follow_raw.${key}; + in + if isList val then + acc + // { + ${key} = key; + } + // listToAttrs ( + map (a: { + name = a; + value = key; + }) val + ) + else if isString val then + acc // { ${key} = val; } + else + acc + ) { } (attrNames all_follow_raw); + + knownTypes = [ + "github" + "gitlab" + "git" + "tarball" + "path" + "indirect" + ]; + + # path nodes are convenience pins, so return the live local path directly + # because fetchTree rejects unlocked paths in pure eval + fetchPin = + name: + if !(lock ? ${name}) then + throw "tack: pin '${name}' has no lock entry; run tack update" + else + let + node = lock.${name}; + in + if (node.type or "") == "path" then + { + outPath = if substring 0 1 node.path == "/" then node.path else ./. + ("/" + node.path); + lastModified = node.lastModified or 0; + } + // (if node ? narHash then { inherit (node) narHash; } else { }) + else if !(elem (node.type or "") knownTypes) then + throw "tack: unknown lock type '${node.type or "?"}' for pin '${name}'" + else + fetchTree node; + + fetchFixed = + name: entry: + let + raw = derivation { + inherit name; + inherit (entry) url; + builder = "builtin:fetchurl"; + system = "builtin"; + outputHash = entry.sha256; + outputHashAlgo = "sha256"; + outputHashMode = "flat"; + }; + unpacked = derivation { + inherit name; + builder = "builtin:unpack-channel"; + system = "builtin"; + src = raw; + channelName = name; + }; + in + if (entry.unpack or "file") == "tarball" then unpacked.outPath + "/" + name else raw.outPath; + + resolveSpec = upLock: spec: if isList spec then walkPath upLock upLock.root spec else spec; + + walkPath = + upLock: nodeName: path: + if path == [ ] then + nodeName + else if !(upLock.nodes ? ${nodeName}) then + throw "tack: follows path dead-end: no node '${nodeName}' in flake.lock" + else + let + key = head path; + inputs = upLock.nodes.${nodeName}.inputs or { }; + in + if !(inputs ? ${key}) then + throw "tack: follows path dead-end: node '${nodeName}' has no input '${key}'" + else + walkPath upLock (resolveSpec upLock inputs.${key}) (tail path); + + followsFor = + pin: + let + rules = removeAttrs all_follow (pin.exclude_follow or [ ]); + in + { + level = rules // (pin.follows or { }); + deep = rules; + }; + + resolveFollows = mapAttrs ( + _: target: self.${target} or (throw "tack: follows target '${target}' is not a pin") + ); + + # follows key is `flake:name`, `tack:name`, or bare `name` + # project onto one side, rekeyed to bare names + followsForSide = + side: follows: + listToAttrs ( + concatMap ( + key: + let + m = match "(flake|tack):(.*)" key; + in + if m == null then + [ + { + name = key; + value = follows.${key}; + } + ] + else if head m == side then + [ + { + name = elemAt m 1; + value = follows.${key}; + } + ] + else + [ ] + ) (attrNames follows) + ); + + mkCallerInputs = + upLock: nodeName: rawInputs: levelFollows: deepFollows: + let + resolved = resolveFollows levelFollows; + in + mapAttrs ( + n: _decl: + resolved.${n} or ( + if upLock != null then + let + ref = + (upLock.nodes.${nodeName}.inputs or { }).${n} + or (throw "tack: input '${n}' declared but not in flake.lock node '${nodeName}'"); + childName = resolveSpec upLock ref; + childNode = upLock.nodes.${childName}; + childSrc = fetchTree childNode.locked; + in + if childNode.flake or true then evalTransitive upLock childName childSrc deepFollows else childSrc + else + throw "tack: no flake.lock; cannot resolve input '${n}'" + ) + ) rawInputs; + + mkFlakeResult = + sourceInfo: flakeDir: callerInputs: outputs: + outputs + // sourceInfo + // { + outPath = flakeDir; + inputs = callerInputs; + inherit outputs sourceInfo; + _type = "flake"; + }; + + evalFlake = + sourceInfo: flakeDir: upLock: nodeName: levelFollows: deepFollows: + let + raw = import (flakeDir + "/flake.nix"); + + tackPinsPath = flakeDir + "/.tack/pins.toml"; + hasTack = pathExists tackPinsPath; + upPins = if hasTack then fromTOML (readFile tackPinsPath) else { }; + + # project follows onto each side, keep only names that side has + # bare follow reaches both; `flake:`/`tack:` reaches just one + tackOverrides = resolveFollows ( + intersectAttrs (upPins.inputs or { }) (followsForSide "tack" levelFollows) + ); + flakeLevel = intersectAttrs (raw.inputs or { }) (followsForSide "flake" levelFollows); + + # deep follows pass down raw, so each descendant re-projects per side + callerInputs = mkCallerInputs upLock nodeName (raw.inputs or { }) flakeLevel deepFollows; + + # upstream declares its outputs forward tackOverrides; a closed `{ self }:` + # would throw on the extra kwarg, so forward only when declared + supportsOverrides = (upPins.tack or { }).recomposable or false; + + extraArgs = if supportsOverrides && tackOverrides != { } then { inherit tackOverrides; } else { }; + + outputs = raw.outputs (callerInputs // extraArgs // { self = result; }); + + result = + let + base = mkFlakeResult sourceInfo flakeDir callerInputs outputs; + in + if hasTack && tackOverrides != { } && !supportsOverrides then + trace "tack: ${flakeDir}: not marked recomposable (set [tack] recomposable = true); overrides will not reach upstream" base + else + base; + in + result; + + evalTransitive = + upLock: nodeName: sourceInfo: follows: + evalFlake sourceInfo sourceInfo.outPath upLock nodeName follows follows; + + evalTopFlake = + sourceInfo: pin: + let + flakeDir = sourceInfo.outPath + (if pin ? dir then "/" + pin.dir else ""); + upLockPath = flakeDir + "/flake.lock"; + upLock = if pathExists upLockPath then fromJSON (readFile upLockPath) else null; + rootNode = if upLock != null then upLock.root else null; + f = followsFor pin; + in + evalFlake sourceInfo flakeDir upLock rootNode f.level f.deep; + + evalFetch = + sourceInfo: pin: subdir: + let + path = sourceInfo.outPath + subdir; + tackPinsPath = path + "/.tack/pins.toml"; + hasTack = pathExists tackPinsPath; + upPins = if hasTack then fromTOML (readFile tackPinsPath) else { }; + f = followsFor pin; + # a fetch drill-in is tack-only + tackOverrides = resolveFollows ( + intersectAttrs (upPins.inputs or { }) (followsForSide "tack" f.level) + ); + in + # a fetch pin is a source tree (path); hand back resolved inputs only when + # there are overrides to push into the upstream's .tack + if hasTack && tackOverrides != { } then + let + upstream = import (path + "/.tack"); + in + # old resolvers return a plain attrset, not a callable functor + if upstream ? __functor then + (upstream { overrides = tackOverrides; }) // { outPath = path; } + else + trace "tack: ${path}: upstream .tack predates override support; overrides will not reach it" path + else + path; + + loadPin = + name: pin: + let + pinType = pin.type or (if pin.flake or true then "flake" else "fetch"); + subdir = if pin ? dir then "/" + pin.dir else ""; + in + if pinType == "fixed" then + fetchFixed name lock.${name} + else + let + sourceInfo = fetchPin name; + in + if pinType == "flake" then evalTopFlake sourceInfo pin else evalFetch sourceInfo pin subdir; + + declared = pins.inputs or { }; + + # undeclared lock entries are auto-dedup synthetics only when referenced as + # [all_follow] targets; stale locks from hand-edits are ignored (tack rm to clean) + autoTargets = listToAttrs ( + map (target: { + name = target; + value = true; + }) (attrValues all_follow) + ); + autoNames = filter (n: !(declared ? ${n}) && autoTargets ? ${n}) (attrNames lock); + autoPin = + name: + let + sourceInfo = fetchPin name; + in + if pathExists (sourceInfo.outPath + "/flake.nix") then evalTopFlake sourceInfo { } else sourceInfo; + + self = + (mapAttrs loadPin declared) + // listToAttrs ( + map (name: { + inherit name; + value = autoPin name; + }) autoNames + ) + // overrides; + in + self // { __functor = _: call; }; +in +call { } diff --git a/.tack/pins.lock.json b/.tack/pins.lock.json new file mode 100644 index 0000000..2858750 --- /dev/null +++ b/.tack/pins.lock.json @@ -0,0 +1,26 @@ +{ + "crane": { + "type": "github", + "owner": "ipetkov", + "repo": "crane", + "rev": "b8d87f76ea4e4b6f30f2e6eaf3fbb60ff7d87167", + "narHash": "sha256-H++/2dMt+5+tErKU+t5z6c4Xedvj9w+UPQYrpygwIPw=", + "lastModified": 1785631643 + }, + "nixpkgs": { + "type": "github", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a5cbcfe954791221bfffe2307f7d1a1bf61a871e", + "narHash": "sha256-z7D96eESRM4CPV/XtwpwFn8IDdfLAmxz6lVWrGYXvR4=", + "lastModified": 1785602060 + }, + "rust-overlay": { + "type": "github", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "32346a8154e65fa10bac36f7b476a5294cb8b150", + "narHash": "sha256-4wZa7NDOxbcm9pTNNcg88nvvPb9uVcWsJ+Ncil8eB1I=", + "lastModified": 1785648791 + } +} diff --git a/.tack/pins.toml b/.tack/pins.toml new file mode 100644 index 0000000..b36d0a3 --- /dev/null +++ b/.tack/pins.toml @@ -0,0 +1,11 @@ +[inputs.nixpkgs] +url = "github:NixOS/nixpkgs/nixpkgs-unstable" +type = "fetch" + +[inputs.crane] +url = "github:ipetkov/crane/master" +type = "fetch" + +[inputs.rust-overlay] +url = "github:oxalica/rust-overlay/master" +type = "fetch" diff --git a/flake.nix b/flake.nix index c800c45..7a0c819 100644 --- a/flake.nix +++ b/flake.nix @@ -1,11 +1,11 @@ { description = "The format that's super!"; - inputs = { }; - outputs = - { self, ... }: + { self, ... }@args: let + inputs = import ./.tack { overrides = args.tackOverrides or { }; }; + systems = [ "x86_64-linux" "aarch64-linux" @@ -32,8 +32,21 @@ eachSystem ( system: let - inherit (import ./nix { inherit system; }) - pkgs + pkgs = import inputs.nixpkgs { + inherit system; + overlays = [ (import inputs.rust-overlay) ]; + }; + craneLib = (import inputs.crane { inherit pkgs; }).overrideToolchain ( + p: + p.rust-bin.stable.latest.default.override { + targets = [ + "wasm32-unknown-unknown" + "wasm32-wasip2" + ]; + } + ); + + inherit (import ./nix { inherit system pkgs craneLib; }) packages checks shell diff --git a/nix/default.nix b/nix/default.nix index 523f863..fbe6381 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -1,11 +1,11 @@ { system ? builtins.currentSystem, - sources ? import ../npins, - pkgs ? import sources.nixpkgs { + inputs ? import ../.tack, + pkgs ? import inputs.nixpkgs { inherit system; - overlays = [ (import sources.rust-overlay) ]; + overlays = [ (import inputs.rust-overlay) ]; }, - craneLib ? (import sources.crane { inherit pkgs; }).overrideToolchain ( + craneLib ? (import inputs.crane { inherit pkgs; }).overrideToolchain ( p: p.rust-bin.stable.latest.default.override { targets = [ @@ -156,7 +156,6 @@ in shell = craneLib.devShell { packages = [ pkgs.binaryen - pkgs.bun pkgs.cargo-insta pkgs.dotnet-sdk_8 pkgs.jdk21_headless @@ -164,6 +163,7 @@ in pkgs.llvmPackages.bintools pkgs.nodejs_24 pkgs.python3 + pkgs.tack pkgs.tree-sitter pkgs.uv pkgs.wasm-bindgen-cli_0_2_100 diff --git a/npins/default.nix b/npins/default.nix deleted file mode 100644 index 884fc8c..0000000 --- a/npins/default.nix +++ /dev/null @@ -1,249 +0,0 @@ -/* - This file is provided under the MIT licence: - - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ -# Generated by npins. Do not modify; will be overwritten regularly -let - # Backwards-compatibly make something that previously didn't take any arguments take some - # The function must return an attrset, and will unfortunately be eagerly evaluated - # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments - mkFunctor = - fn: - let - e = builtins.tryEval (fn { }); - in - (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; }; - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 - range = - first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 - stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 - stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); - concatStrings = builtins.concatStringsSep ""; - - # If the environment variable NPINS_OVERRIDE_${name} is set, then use - # the path directly as opposed to the fetched source. - # (Taken from Niv for compatibility) - mayOverride = - name: path: - let - envVarName = "NPINS_OVERRIDE_${saneName}"; - saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name; - ersatz = builtins.getEnv envVarName; - in - if ersatz == "" then - path - else - # this turns the string into an actual Nix path (for both absolute and - # relative paths) - builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" ( - if builtins.substring 0 1 ersatz == "/" then - /. + ersatz - else - /. + builtins.getEnv "PWD" + "/${ersatz}" - ); - - mkSource = - name: spec: - { - pkgs ? null, - }: - assert spec ? type; - let - # Unify across builtin and pkgs fetchers. - # `fetchGit` requires a wrapper because of slight API differences. - fetchers = - if pkgs == null then - { - inherit (builtins) fetchTarball fetchurl; - # For some fucking reason, fetchGit has a different signature than the other builtin fetchers … - fetchGit = args: (builtins.fetchGit args).outPath; - } - else - { - fetchTarball = - { - url, - sha256, - }: - pkgs.fetchzip { - inherit url sha256; - extension = "tar"; - }; - inherit (pkgs) fetchurl; - fetchGit = - { - url, - submodules, - rev, - name, - narHash, - }: - pkgs.fetchgit { - inherit url rev name; - fetchSubmodules = submodules; - hash = narHash; - }; - }; - - # Dispatch to the correct code path based on the type - path = - if spec.type == "Git" then - mkGitSource fetchers spec - else if spec.type == "GitRelease" then - mkGitSource fetchers spec - else if spec.type == "PyPi" then - mkPyPiSource fetchers spec - else if spec.type == "Channel" then - mkChannelSource fetchers spec - else if spec.type == "Tarball" then - mkTarballSource fetchers spec - else if spec.type == "Container" then - mkContainerSource pkgs spec - else - builtins.throw "Unknown source type ${spec.type}"; - in - spec // { outPath = mayOverride name path; }; - - mkGitSource = - { - fetchTarball, - fetchGit, - ... - }: - { - repository, - revision, - url ? null, - submodules, - hash, - ... - }: - assert repository ? type; - # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository - # In the latter case, there we will always be an url to the tarball - if url != null && !submodules then - fetchTarball { - inherit url; - sha256 = hash; - } - else - let - url = - if repository.type == "Git" then - repository.url - else if repository.type == "GitHub" then - "https://github.com/${repository.owner}/${repository.repo}.git" - else if repository.type == "GitLab" then - "${repository.server}/${repository.repo_path}.git" - else if repository.type == "Forgejo" then - "${repository.server}/${repository.owner}/${repository.repo}.git" - else - throw "Unrecognized repository type ${repository.type}"; - urlToName = - url: rev: - let - matched = builtins.match "^.*/([^/]*)(\\.git)?$" url; - - short = builtins.substring 0 7 rev; - - appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; - in - "${if matched == null then "source" else builtins.head matched}${appendShort}"; - name = urlToName url revision; - in - fetchGit { - rev = revision; - narHash = hash; - - inherit name submodules url; - }; - - mkPyPiSource = - { fetchurl, ... }: - { - url, - hash, - ... - }: - fetchurl { - inherit url; - sha256 = hash; - }; - - mkChannelSource = - { fetchTarball, ... }: - { - url, - hash, - ... - }: - fetchTarball { - inherit url; - sha256 = hash; - }; - - mkTarballSource = - { fetchTarball, ... }: - { - url, - locked_url ? url, - hash, - ... - }: - fetchTarball { - url = locked_url; - sha256 = hash; - }; - - mkContainerSource = - pkgs: - { - image_name, - image_tag, - image_digest, - ... - }: - if pkgs == null then - builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers" - else - pkgs.dockerTools.pullImage { - imageName = image_name; - imageDigest = image_digest; - finalImageTag = image_tag; - }; -in -mkFunctor ( - { - input ? ./sources.json, - }: - let - data = - if builtins.isPath input then - # while `readFile` will throw an error anyways if the path doesn't exist, - # we still need to check beforehand because *our* error can be caught but not the one from the builtin - # *piegames sighs* - if builtins.pathExists input then - builtins.fromJSON (builtins.readFile input) - else - throw "Input path ${toString input} does not exist" - else if builtins.isAttrs input then - input - else - throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset"; - version = data.version; - in - if version == 7 then - builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins - else - throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" -) diff --git a/npins/sources.json b/npins/sources.json deleted file mode 100644 index ab216d2..0000000 --- a/npins/sources.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "pins": { - "crane": { - "type": "Git", - "repository": { - "type": "GitHub", - "owner": "ipetkov", - "repo": "crane" - }, - "branch": "master", - "submodules": false, - "revision": "27711550d109bf6236478dc9f53b9e29c1a374c5", - "url": "https://github.com/ipetkov/crane/archive/27711550d109bf6236478dc9f53b9e29c1a374c5.tar.gz", - "hash": "sha256-5fDkKTYEgue2klksd52WvcXfZdY1EIlbk0QggAwpFog=" - }, - "nixpkgs": { - "type": "Channel", - "name": "nixpkgs-unstable", - "url": "https://releases.nixos.org/nixpkgs/nixpkgs-26.05pre958799.917fec990948/nixexprs.tar.xz", - "hash": "sha256-QKxBW7o8gmUwDXQhsbIb0JCoUgCr8B80ltkjbg840UQ=" - }, - "rust-overlay": { - "type": "Git", - "repository": { - "type": "GitHub", - "owner": "oxalica", - "repo": "rust-overlay" - }, - "branch": "master", - "submodules": false, - "revision": "c10801f59c68e14c308aea8fa6b0b3d81d43c61e", - "url": "https://github.com/oxalica/rust-overlay/archive/c10801f59c68e14c308aea8fa6b0b3d81d43c61e.tar.gz", - "hash": "sha256-3FB/WzLZSiU2Mc50C9q9VXU1LRUZbsU6UHKmZG1C+hU=" - } - }, - "version": 7 -} -- 2.51.2