Create a derivation in Nix to only copy some files.

huangapple go评论56阅读模式
英文:

Create a derivation in Nix to only copy some files

问题

我有一些文件在一个文件夹中,我想创建一个派生以将它们作为输入传递给后面的 dockerTools.buildImage

我不确定如何做这个。以下不起作用:

database = pkgs.stdenv.mkDerivation {
  name = "database";
  src = "./";
  phases = [ "installPhase" ];
  installPhase = ''
      pwd
      ls -al
      echo $src
      cp $src/.env $out
      cp $src/20221002.sqlite $out
  '';
};

因为 cp 找不到这些文件。

英文:

I have some files in a folder that I want to create a derivation out of to pass them as inputs to dockerTools.buildImage later.

I'm not sure how to do this. This doesn't work:

database = pkgs.stdenv.mkDerivation {
  name = "database";
  src = "./";
  phases = [ "installPhase" ];
  installPhase = ''
      pwd
      ls -al
      echo $src
      cp $src/.env $out
      cp $src/20221002.sqlite $out
  '';
};

Because cp can't find the files.

答案1

得分: 1

数据库 = pkgs.stdenv.mkDerivation {
  名称 = "数据库";
  src = ./.;
  阶段 = [ "unpackPhase" "installPhase" ];
  installPhase = '''
    mkdir -p $out
    cp $src/.env $out
    cp $src/20221002.sqlite $out
  ''';
};
英文:

Turns out this is what's necessary for this to work:

database = pkgs.stdenv.mkDerivation {
  name = "database";
  src = ./.;
  phases = [ "unpackPhase" "installPhase" ];
  installPhase = ''
    mkdir -p $out
    cp $src/.env $out
    cp $src/20221002.sqlite $out
  '';
};

The src attribute needs to point to the current directory and then everything in there will be copied over.

The copying only happens when unpackPhase is part of the phases.

Then in the installPhase we need to drop into shell to do the actual work. $out means the output path of the derivation and $src is set to the folder of the src. In many cases $out is just a file but in this case since we want it to contain two files, we have to mkdir it first and then we can cp both files into it.

huangapple
  • 本文由 发表于 2023年5月13日 21:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定