Install NixOS on Amazon EC2: Difference between revisions

imported>Fadenb
m Syntax highlighting
imported>Fadenb
m Syntax highlighting: use console lexer instead of bash
Line 21: Line 21:
<li>To create instances from the command line, you need to install the EC2 API tools:
<li>To create instances from the command line, you need to install the EC2 API tools:


<syntaxhighlight lang="bash">$ nix-env -i ec2-api-tools</syntaxhighlight>
<syntaxhighlight lang="console">$ nix-env -i ec2-api-tools</syntaxhighlight>


Alternatively, you can create NixOS instances using the [http://aws.amazon.com/console/ AWS Management Console].
Alternatively, you can create NixOS instances using the [http://aws.amazon.com/console/ AWS Management Console].
Line 27: Line 27:
<li>Start a small instance running NixOS:
<li>Start a small instance running NixOS:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-run-instances -k gsg-keypair ami-c9f2d8bd
$ ec2-run-instances -k gsg-keypair ami-c9f2d8bd
RESERVATION  r-10ca4167  516444698777  default
RESERVATION  r-10ca4167  516444698777  default
Line 34: Line 34:
<tt>gsg-keypair</tt> denotes an SSH key pair created with <tt>ec2-add-keypair</tt>.  To run a 64-bit instance, you must specify a 64-bit-capable instance type, e.g., <tt>-t m1.large</tt>.  If your requirements allow it, you may prefer using a cheap spot instance:
<tt>gsg-keypair</tt> denotes an SSH key pair created with <tt>ec2-add-keypair</tt>.  To run a 64-bit instance, you must specify a 64-bit-capable instance type, e.g., <tt>-t m1.large</tt>.  If your requirements allow it, you may prefer using a cheap spot instance:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-request-spot-instances -t m1.large -k gsg-keypair -p 0.3 ami-ecb49e98</syntaxhighlight>
$ ec2-request-spot-instances -t m1.large -k gsg-keypair -p 0.3 ami-ecb49e98</syntaxhighlight>


<li>After a while the instance should be running, and you can log in using the SSH key generated by <tt>ec2-add-keypair</tt>:
<li>After a while the instance should be running, and you can log in using the SSH key generated by <tt>ec2-add-keypair</tt>:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ssh -i id_rsa-gsg-keypair root@ec2-79-125-97-89.eu-west-1.compute.amazonaws.com</syntaxhighlight>
$ ssh -i id_rsa-gsg-keypair root@ec2-79-125-97-89.eu-west-1.compute.amazonaws.com</syntaxhighlight>


To get the IP address / hostname of the instance, use <tt>ec2-describe-instances</tt>.  You may need to allow traffic to port 22 (ssh):
To get the IP address / hostname of the instance, use <tt>ec2-describe-instances</tt>.  You may need to allow traffic to port 22 (ssh):


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-authorize default -p 22 -s 0.0.0.0/0</syntaxhighlight>
$ ec2-authorize default -p 22 -s 0.0.0.0/0</syntaxhighlight>


<li>To start working with the instance, you may want to do the following to obtain the Nixpkgs and NixOS sources:
<li>To start working with the instance, you may want to do the following to obtain the Nixpkgs and NixOS sources:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ nixos-checkout
$ nixos-checkout
$ nixos-rebuild pull</syntaxhighlight>
$ nixos-rebuild pull</syntaxhighlight>
Line 55: Line 55:
You should now be able to install software, e.g.
You should now be able to install software, e.g.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ nix-env -i emacs</syntaxhighlight>
$ nix-env -i emacs</syntaxhighlight>


or reconfigure the instance:
or reconfigure the instance:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ nano /etc/nixos/configuration.nix
$ nano /etc/nixos/configuration.nix
$ nixos-rebuild switch</syntaxhighlight>
$ nixos-rebuild switch</syntaxhighlight>
Line 79: Line 79:
<li>Install the EC2 AMI tools:
<li>Install the EC2 AMI tools:


<syntaxhighlight lang="bash">$ nix-env -i ec2-ami-tools</syntaxhighlight>
<syntaxhighlight lang="console">$ nix-env -i ec2-ami-tools</syntaxhighlight>


<li>The easy way to build NixOS AMIs is to run the following command:  
<li>The easy way to build NixOS AMIs is to run the following command:  


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ NIXOS=/path/to/nixos NIXPKGS=/path/to/nixpkgs /path/to/nixos/maintainers/scripts/create-amis.sh</syntaxhighlight>
$ NIXOS=/path/to/nixos NIXPKGS=/path/to/nixpkgs /path/to/nixos/maintainers/scripts/create-amis.sh</syntaxhighlight>


Line 92: Line 92:
<li>Build the raw disk image:
<li>Build the raw disk image:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ NIXPKGS=/path/to/nixpkgs NIXOS_CONFIG=/path/to/nixos/modules/virtualisation/amazon-config.nix \
$ NIXPKGS=/path/to/nixpkgs NIXOS_CONFIG=/path/to/nixos/modules/virtualisation/amazon-config.nix \
     nix-build /path/to/nixos -A config.system.build.amazonImage \
     nix-build /path/to/nixos -A config.system.build.amazonImage \
Line 99: Line 99:
(Use <tt>--argstr system x86_64-linux</tt> to build a 64-bit image.) This will produce an <tt>ext3</tt> disk image in <tt>./result/nixos.img</tt>:
(Use <tt>--argstr system x86_64-linux</tt> to build a 64-bit image.) This will produce an <tt>ext3</tt> disk image in <tt>./result/nixos.img</tt>:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ls -l ./result/nixos.img  
$ ls -l ./result/nixos.img  
-r--r--r-- 1 root nixbld 4294967296 Jan  1  1970 ./result/nixos.img</syntaxhighlight>
-r--r--r-- 1 root nixbld 4294967296 Jan  1  1970 ./result/nixos.img</syntaxhighlight>
Line 105: Line 105:
<li>Bundle the image:
<li>Bundle the image:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-bundle-image -i ./result/nixos.img --user $AWS_ACCOUNT --arch i386 \
$ ec2-bundle-image -i ./result/nixos.img --user $AWS_ACCOUNT --arch i386 \
     -c $EC2_CERT -k $EC2_PRIVATE_KEY \
     -c $EC2_CERT -k $EC2_PRIVATE_KEY \
Line 123: Line 123:
<li>Upload the bundle:
<li>Upload the bundle:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-upload-bundle -b nixos-img -m /tmp/nixos.img.manifest.xml \
$ ec2-upload-bundle -b nixos-img -m /tmp/nixos.img.manifest.xml \
     -a $AWS_ACCESS_KEY_ID -s $AWS_SECRET_ACCESS_KEY --location EU
     -a $AWS_ACCESS_KEY_ID -s $AWS_SECRET_ACCESS_KEY --location EU
Line 141: Line 141:
<li>Register the image as a private AMI:
<li>Register the image as a private AMI:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-register nixos-img/nixos.img.manifest.xml -n nixos-img -d '...description...'
$ ec2-register nixos-img/nixos.img.manifest.xml -n nixos-img -d '...description...'
IMAGE  ami-efffd59b</syntaxhighlight>
IMAGE  ami-efffd59b</syntaxhighlight>
Line 147: Line 147:
To make it public:
To make it public:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ ec2-modify-image-attribute ami-efffd59b -l -a all</syntaxhighlight>
$ ec2-modify-image-attribute ami-efffd59b -l -a all</syntaxhighlight>