在具有两个 Nix Flake 的设置中,其中一个为另一个的应用程序提供插件,"path <…> is not valid"。如何解决?

In a setup with two Nix Flakes, where one provides a plugin for the other's application, "path <…> is not valid". How to fix that?

我有两个 Nix Flakes:一个包含一个应用程序,另一个包含该应用程序的插件。当我使用插件构建应用程序时,出现错误

error: path '/nix/store/3b7djb5pr87zbscggsr7vnkriw3yp21x-mainapp-go-modules' is not valid

我不知道这个错误是什么意思以及如何修复它,但我可以在 macOS 和 Linux 上重现它。问题路径是buildGoModule第一步生成的vendor目录。

重现错误的最小设置需要一堆文件,因此我提供了一个注释 bash 脚本,您可以在空文件夹中执行该脚本以重新创建我的设置:

#!/bin/bash

# I have two flakes: the main application and a plugin.
# the mainapp needs to be inside the plugin directory
# so that nix doesn't complain about the path:mainapp
# reference being outside the parent's root.
mkdir -p plugin/mainapp

# each is a go module with minimal setup
tee plugin/mainapp/go.mod <<EOF >/dev/null
module example.com/mainapp

go 1.16
EOF
tee plugin/go.mod <<EOF >/dev/null
module example.com/plugin

go 1.16
EOF

# each contain minimal Go code
tee plugin/mainapp/main.go <<EOF >/dev/null
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
EOF
tee plugin/main.go <<EOF >/dev/null
package plugin

import log

func init() {
    fmt.Println("initializing plugin")
}
EOF

# the mainapp is a flake that provides a function for building
# the app, as well as a default package that is the app
# without any plugins.
tee plugin/mainapp/flake.nix <<'EOF' >/dev/null
{
  description = "main application";
  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11;
    flake-utils.url = github:numtide/flake-utils;
  };
  outputs = {self, nixpkgs, flake-utils}:
  let
    # buildApp builds the application from a list of plugins.
    # plugins cause the vendorSha256 to change, hence it is
    # given as additional parameter.
    buildApp = { pkgs, vendorSha256, plugins ? [] }:
      let
        # this is appended to the mainapp's go.mod so that it
        # knows about the plugin and where to find it.
        requirePlugin = plugin: ''
          require ${plugin.goPlugin.goModName} v0.0.0
          replace ${plugin.goPlugin.goModName} => ${plugin.outPath}/src
        '';
        # since buildGoModule consumes the source two times –
        # first for vendoring, and then for building –
        # we do the necessary modifications to the sources in an
        # own derivation and then hand that to buildGoModule.
        sources = pkgs.stdenvNoCC.mkDerivation {
          name = "mainapp-with-plugins-source";
          src = self;
          phases = [ "unpackPhase" "buildPhase" "installPhase" ];
          # write a plugins.go file that references the plugin's package via
          #   _ = "<module path>"
          PLUGINS_GO = ''
            package main
              
            // Code generated by Nix. DO NOT EDIT.
          
            import (
                ${builtins.foldl' (a: b: a + "\n\t_ = \"${b.goPlugin.goModName}\"") "" plugins}
            )
          '';
          GO_MOD_APPEND = builtins.foldl' (a: b: a + "${requirePlugin b}\n") "" plugins;
          buildPhase = ''
            printenv PLUGINS_GO >plugins.go
            printenv GO_MOD_APPEND >>go.mod
          '';
          installPhase = ''
            mkdir -p $out
            cp -r -t $out *
          '';
        };
      in pkgs.buildGoModule {
        name = "mainapp";
        src = builtins.trace "sources at ${sources}" sources;
        inherit vendorSha256;
        nativeBuildInputs = plugins;
      };
  in (flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = nixpkgs.legacyPackages.${system};
    in rec {
      defaultPackage = buildApp {
        inherit pkgs;
        # this may be different depending on your nixpkgs; if it is, just change it.
        vendorSha256 = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo=";
      };
    }
  )) // {
    lib = {
      inherit buildApp;
      # helper that parses a go.mod file for the module's name
      pluginMetadata = goModFile: {
        goModName = with builtins; head
          (match "module ([^[:space:]]+).*" (readFile goModFile));
      };
    };
  };
}
EOF

# the plugin is a flake depending on the mainapp that outputs a plugin package,
# and also a package that is the mainapp compiled with this plugin.
tee plugin/flake.nix <<'EOF' >/dev/null
{
  description = "mainapp plugin";
  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11;
    flake-utils.url = github:numtide/flake-utils;
    nix-filter.url = github:numtide/nix-filter;
    mainapp.url = path:mainapp;
    mainapp.inputs = {
      nixpkgs.follows = "nixpkgs";
      flake-utils.follows = "flake-utils";
    };
  };
  outputs = {self, nixpkgs, flake-utils, nix-filter, mainapp}:
  flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = nixpkgs.legacyPackages.${system};
    in rec {
      packages = rec {
        plugin = pkgs.stdenvNoCC.mkDerivation {
          pname = "mainapp-plugin";
          version = "0.1.0";
          src = nix-filter.lib.filter {
            root = ./.;
            exclude = [ ./mainapp ./flake.nix ./flake.lock ];  
          };
          # needed for mainapp to recognize this as plugin
          passthru.goPlugin = mainapp.lib.pluginMetadata ./go.mod;
          phases = [ "unpackPhase" "installPhase" ];
          installPhase = ''
            mkdir -p $out/src
            cp -r -t $out/src *
          '';
        };
        app = mainapp.lib.buildApp {
          inherit pkgs;
          # this may be different depending on your nixpkgs; if it is, just change it.
          vendorSha256 = "sha256-a6HFGFs1Bu9EkXwI+DxH5QY2KBcdPzgP7WX6byai4hw=";
          plugins = [ plugin ];
        };
      };
      defaultPackage = packages.app;
    }
  );
}
EOF

您需要安装了支持 Flake 的 Nix 才能重现错误。 在这个脚本创建的plugin文件夹中,执行

$ nix build
trace: sources at /nix/store/d5arinbiaspyjjc4ypk4h5dsjx22pcsf-mainapp-with-plugins-source
error: path '/nix/store/3b7djb5pr87zbscggsr7vnkriw3yp21x-mainapp-go-modules' is not valid

(如果哈希值不匹配,只需使用正确的哈希值更新薄片;我不太确定在存储库外传播薄片时的哈希是否可重现。)

sources 目录(由跟踪显示)确实存在并且看起来没问题。错误消息中给出的路径也存在并且包含 modules.txt 和预期的内容。

在文件夹 mainapp 中,nix build 成功地 运行,构建了没有插件的应用程序。那么我对使路径无效的插件做了什么?

原因是在这种情况下,作为销售一部分生成的文件 modules.txt 将在 replace 指令中包含 nix 存储路径。 vendor 目录是一个固定的输出派生,因此不能依赖于任何其他派生。 modules.txt.

中的引用违反了这一点

这只能通过将插件的源代码复制到 sources 派生中来解决——这样,replace 路径可以是相对的,因此不会引用其他 nix 存储路径。