Open bbigras opened 3 years ago
Maybe it's because I forgot to run terraform init
.
Ah no I have the same message.
It's as the error says, nix-instantiate
needs to be installed on the machine that runs Terraform. This can also happen when running nix-shell --pure
if Nix is not part of the shell's runtime closure.
I'm running this on NixOS. nix-instantiate
is in my path and I'm not using nix-shell
.
Are you using Terraform Cloud by any chance? If the execution is done on the remote worker it won't have nix-instantiate installed.
Yes, sorry. I didn't realize I was using it.
I'm guessing there's no way around that.
Thanks.
Would you be interested in investigating this if it was possible? The local-exec provisioned could be added on a null_recource to pull a static version of nix. See https://twitter.com/zimbatm/status/1359160894249385988?s=20
resource "null_resource" "cluster" {
provisioner "local-exec" {
command = "sh <(curl -L https://github.com/numtide/nix-flakes-installer/releases/download/nix-2.4pre20210126_f15f0b8/install)"
interpreter = ["bash", "-c"]
}
}
module "deploy_nixos" {
source = "git::https://github.com/tweag/terraform-nixos.git//deploy_nixos?ref=5f5a0408b299874d6a29d1271e9bffeee4c9ca71"
nixos_config = "${path.module}/configuration.nix"
target_host = aws_instance.machine.public_ip
ssh_private_key_file = local_file.machine_ssh_key.filename
ssh_agent = false
depends_on = [ "null_resource.cluster" ]
}
Error: Error running command 'sh <(curl -L https://github.com/numtide/nix-flakes-installer/releases/download/nix-2.4pre20210126_f15f0b8/install)': exit status 1. Output: % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 615 100 615 0 0 4300 0 --:--:-- --:--:-- --:--:-- 4300
100 3865 100 3865 0 0 15775 0 --:--:-- --:--:-- --:--:-- 15775
/dev/fd/63: 63: /dev/fd/63: --tarball-url-prefix: not found
downloading Nix 2.4pre20210126_f15f0b8 binary tarball for x86_64-linux from 'https://github.com/numtide/nix-flakes-installer/releases/download/nix-2.4pre20210126_f15f0b8/nix-2.4pre20210126_f15f0b8-x86_64-linux.tar.xz' to '/tmp/nix-binary-tarball-unpack.Khl0ernFOe'...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 654 100 654 0 0 5190 0 --:--:-- --:--:-- --:--:-- 5232
100 17.0M 100 17.0M 0 0 14.8M 0 0:00:01 0:00:01 --:--:-- 20.9M
Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation
performing a single-user installation of Nix...
directory /nix does not exist; creating it by running 'mkdir -m 0755 /nix && chown terraform /nix' using sudo
sudo: unable to stat /etc/sudoers: No such file or directory
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
/tmp/nix-binary-tarball-unpack.Khl0ernFOe/unpack/nix-2.4pre20210126_f15f0b8-x86_64-linux/install: please manually run 'mkdir -m 0755 /nix && chown terraform /nix' as root to create /nix
Terraform Cloud does not allow you to elevate a command's permissions with sudo during Terraform runs. This means you cannot install packages using the worker OS's normal package management tools. However, you can install and execute standalone binaries in Terraform's working directory.
https://www.terraform.io/docs/cloud/run/install-software.html#only-install-standalone-binaries
It's a bit of uncharted territory so I am not sure it will work. Ideally something like this would work:
exec -a "$0" ./buildStatic.x86_64-linux --store /tmp/nix "$@"
.Unfortunately, --store
wasn't working great in my previous attempts.
Another approach would be to use nix-user-chroot.
Another thing to figure out is if Terraform Cloud provides any user-writable location in the PATH. If not, deploy_nixos could be extended with an option to set the PATH, or point to the folder that contains the Nix installation.
Could https://github.com/DavHau/nix-portable also help with this?
Yes, that might also be a good fallback
I tried but got some error.
null_resource.cluster (local-exec): proot error: '/home/terraform/.nix-portable/store/8yx9ys5a40vg5r8hk14qlhrfgapmic3v-nix-2.4pre20210205_480426a/bin/63' not found (root = /home/terraform/.nix-portable/emptyroot, cwd = /terraform, $PATH=(null))
terraform {
backend "remote" {
organization = "bbigras"
workspaces {
name = "test-nix-portable"
}
}
}
resource "null_resource" "cluster" {
provisioner "local-exec" {
command = "curl https://gitlab.com/proot/proot/-/jobs/981080842/artifacts/raw/dist/proot > proot"
}
provisioner "local-exec" {
command = "chmod u+x proot"
}
provisioner "local-exec" {
command = "ls -l /home/terraform"
}
provisioner "local-exec" {
command = "/terraform/proot --help"
}
provisioner "local-exec" {
command = "ls -l"
}
provisioner "local-exec" {
command = "pwd"
}
provisioner "local-exec" {
command = "bash <(curl -L https://github.com/DavHau/nix-portable/releases/download/v003/nix-portable)"
interpreter = ["bash", "-c"]
environment = {
NP_PROOT = "/terraform/proot"
NP_RUNTIME = "proot"
NP_DEBUG = "1"
# NP_RUNTIME = "bwrap"
}
}
}
provider "aws" {
region = "ca-central-1"
}
module "nixos_image" {
source = "git::https://github.com/tweag/terraform-nixos.git//aws_image_nixos?ref=5f5a0408b299874d6a29d1271e9bffeee4c9ca71"
release = "20.09"
}
resource "aws_instance" "machine" {
ami = module.nixos_image.ami
instance_type = "t3.micro"
root_block_device {
volume_size = 50 # GiB
}
}
module "deploy_nixos" {
source = "git::https://github.com/tweag/terraform-nixos.git//deploy_nixos?ref=5f5a0408b299874d6a29d1271e9bffeee4c9ca71"
target_host = aws_instance.machine.public_ip
ssh_agent = false
depends_on = [ null_resource.cluster ]
}
Are you using Terraform Cloud by any chance? If the execution is done on the remote worker it won't have nix-instantiate installed.
Terraform Cloud is what the tutorial referencing this module recommends using; I'll ping @domenkozar as it looks like he is in charge of the nix.dev site.
https://nix.dev recommends "CLI-driven workflow" option, which means you deploy via a command line but still have logs, etc in the terraform cloud.
https://nix.dev recommends "CLI-driven workflow" option, which means you deploy via a command line but still have logs, etc in the terraform cloud.
Following the workflow detailed at https://nix.dev/tutorials/deploying-nixos-using-terraform.html results in a
Error: failed to execute ".terraform/modules/deploy_nixos/deploy_nixos/nixos-instantiate.sh": running (instantiating): 'nix-instantiate' '--show-trace' '--expr' $'\n { system, configuration, ... }:\n let\n os = import <nixpkgs/nixos> { inherit system configuration; };\n inherit (import <nixpkgs/lib>) concatStringsSep;\n in {\n substituters = concatStringsSep " " os.config.nix.binaryCaches;\n trusted-public-keys = concatStringsSep " " os.config.nix.binaryCachePublicKeys;\n drv_path = os.system.drvPath;\n out_path = os.system;\n inherit (builtins) currentSystem;\n }' '--argstr' 'configuration' '/terraform/configuration.nix' '--argstr' 'system' 'x86_64-linux' -A out_path
.terraform/modules/deploy_nixos/deploy_nixos/nixos-instantiate.sh: line 44: nix-instantiate: command not found
Both @bbigras and I have encountered this error. Perhaps the article can be updated with the method you've indicated?
@worldofgeese where are you running that command, locally? It requires to have Nix installed (that should be added).
@domenkozar from my local host that has nix-instantiate
on $PATH as well as a full Nix installation
What I can see is the tutorial, which is excellently written, states to use Terraform Cloud as a state/locking backend. As a user, I would have liked to see mention of changing from Remote to Local execution under General Settings of the workspace:
@worldofgeese I've added step 5 to https://deploy-preview-125--nixdev.netlify.app/tutorials/deploying-nixos-using-terraform.html#booting-nixos-image - do you think it's correct now?
@domenkozar looks great to me! Thank you!
Describe the bug
To Reproduce
Follow the guide at https://nixos.org/guides/deploying-nixos-using-terraform.html (it's at the part with the configuration.nix file).
Expected behavior
Environment
"x86_64-linux"
Linux 5.10.1-zen1, NixOS, 21.03.20210109.257cbbc (Okapi)
yes
yes
nix-env (Nix) 2.4pre20201205_a5d85d0
"nixos-21.03pre260232.733e537a8ad"
"home-manager-20.09"
/nix/var/nix/profiles/per-user/root/channels/nixos
Terraform v0.14.4
I tried with 5f5a0408b299874d6a29d1271e9bffeee4c9ca71 and f0f623208944c80639ccbc9a56b45e72a6cfd26e.
Additional context