Deploing a single bash script with nixops

989 views Asked by At

I'm just starting to learn nix / nixos / nixops. I needed to install a simple bash script to remote host with nixops. And I can not realize how to do it. I have two files:

  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/
      '';

  }

I deploy as follows. I go to the directory in which these two files are located and then sequentially execute two commands:

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

Deployment passes without errors and completes successfully. But when I login to the newly created remote host, I find that the tmux package from the standard set is present in the system, and my-package is absent:

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:~]#

Help me figure out what's wrong with my scripts. Any links to documentation or examples of the implementation of such a task are welcome.

1

There are 1 answers

0
Robert Hensing On

The shell can not find your script because it is copied into the wrong directory.

This becomes apparent after building my-package.nix:

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

Here you see the basename of a storepath inside a store path. This is caused by the line:

cp -r ${script} $out/

Changing it to something like this should fix that problem:

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