【问题标题】:Deploing a single bash script with nixops使用 nixops 部署单个 bash 脚本
【发布时间】:2020-04-18 20:27:33
【问题描述】:

我刚刚开始学习nix / nixos / nixops。我需要使用nixops 在远程主机上安装一个简单的 bash 脚本。我不知道该怎么做。我有两个文件:

  1. just-deploy-bash-script.nix
{

  resources.sshKeyPairs.ssh-key = {};

  test-host = { config, lib, pkgs, ... }: {
    deployment.targetEnv = "digitalOcean";
    deployment.digitalOcean.region = "sgp1";
    deployment.digitalOcean.size = "s-2vcpu-4gb";


    environment.systemPackages =
    let
      my-package = pkgs.callPackage ./my-package.nix { inherit pkgs; };
    in [
      pkgs.tmux
      my-package
    ];

  };

}
  1. my-package.nix
{ pkgs ? import <nixpkgs> {}, ... }:

let
  pname = "my-package"; 
  version = "1.0.0";
  stdenv = pkgs.stdenv;
in
  stdenv.mkDerivation {

    inherit pname version;

    src = ./.;

    installPhase =
      let
        script = pkgs.writeShellScriptBin "my-test" ''
          echo This is my test script
        '';
      in
      ''
        mkdir $out;
        cp -r ${script} $out/
      '';

  }

我部署如下。我进入这两个文件所在的目录,然后依次执行两个命令:

nixops create -d test just-deploy-bash-script.nix
nixops deploy -d test

部署顺利通过并成功完成。但是当我登录新创建的远程主机时,我发现系统中存在标准集的tmux包,而my-package不存在:

nixops ssh -d test test-host
[root@test-host:~]# which tmux
/run/current-system/sw/bin/tmux

[root@test-host:~]# find /nix/store/ -iname tmux
/nix/store/hd1sgvb4pcllxj69gy3qa9qsns68arda-nixpkgs-20.03pre206749.5a3c1eda46e/nixpkgs/pkgs/tools/misc/tmux
/nix/store/609zdpfi5kpz2c7mbjcqjmpb4sd2y3j4-ncurses-6.0-20170902/share/terminfo/t/tmux
/nix/store/4cxkil2r3dzcf5x2phgwzbxwyvlk6i9k-system-path/share/bash-completion/completions/tmux
/nix/store/4cxkil2r3dzcf5x2phgwzbxwyvlk6i9k-system-path/bin/tmux
/nix/store/606ni2d9614sxkhnnnhr71zqphdam6jc-system-path/share/bash-completion/completions/tmux
/nix/store/606ni2d9614sxkhnnnhr71zqphdam6jc-system-path/bin/tmux
/nix/store/ddlx3x8xhaaj78xr0zasxhiy2m564m2s-nixos-17.09.3269.14f9ee66e63/nixos/pkgs/tools/misc/tmux
/nix/store/kvia4rwy9y4wis4v2kb9y758gj071p5v-ncurses-6.1-20190112/share/terminfo/t/tmux
/nix/store/c3m8qvmn2yxkgpfajjxbcnsgfrcinppl-tmux-2.9a/share/bash-completion/completions/tmux
/nix/store/c3m8qvmn2yxkgpfajjxbcnsgfrcinppl-tmux-2.9a/bin/tmux

[root@test-host:~]# which my-test
which: no my-test in (/root/bin:/run/wrappers/bin:/root/.nix-profile/bin:/etc/profiles/per-user/root/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin)

[root@test-host:~]# find /nix/store/ -iname *my-test*

[root@test-host:~]#

帮助我找出我的脚本出了什么问题。欢迎任何指向此类任务实施的文档或示例的链接。

【问题讨论】:

    标签: nixos nixops


    【解决方案1】:

    shell 找不到你的脚本,因为它被复制到了错误的目录中。

    这在构建 my-package.nix 后变得明显:

    $ nix-build my-package.nix
    $ ls result/
    zh5bxljvpmda4mi4x0fviyavsa3r12cx-my-test
    

    在这里您可以看到存储路径中存储路径的基本名称。这是由以下行引起的:

    cp -r ${script} $out/
    

    把它改成这样应该可以解决这个问题:

    cp -r ${script}/* $out/
    

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 2021-12-25
      • 2020-11-07
      • 2014-12-21
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多