Install NixOS on Amazon EC2: Difference between revisions
imported>Fadenb m →Public NixOS AMIs: fix broken link (ec2-amis.nix was removed from nixops repo, https://github.com/NixOS/nixops/commit/48fc0ffb53c97eb7ea895b0470816b83d0d34647 ) |
imported>Fadenb m Syntax highlighting |
||
| 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> | ||
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"> | ||
$ 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 | ||
INSTANCE i-f6d3b781 ami-c9f2d8bd pending gsg-keypair 0 m1.small ...</ | INSTANCE i-f6d3b781 ami-c9f2d8bd pending gsg-keypair 0 m1.small ...</syntaxhighlight> | ||
<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"> | ||
$ ec2-request-spot-instances -t m1.large -k gsg-keypair -p 0.3 ami-ecb49e98</ | $ 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"> | ||
$ ssh -i id_rsa-gsg-keypair root@ec2-79-125-97-89.eu-west-1.compute.amazonaws.com</ | $ 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"> | ||
$ ec2-authorize default -p 22 -s 0.0.0.0/0</ | $ 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"> | ||
$ nixos-checkout | $ nixos-checkout | ||
$ nixos-rebuild pull</ | $ nixos-rebuild pull</syntaxhighlight> | ||
You should now be able to install software, e.g. | You should now be able to install software, e.g. | ||
< | <syntaxhighlight lang="bash"> | ||
$ nix-env -i emacs</ | $ nix-env -i emacs</syntaxhighlight> | ||
or reconfigure the instance: | or reconfigure the instance: | ||
< | <syntaxhighlight lang="bash"> | ||
$ nano /etc/nixos/configuration.nix | $ nano /etc/nixos/configuration.nix | ||
$ nixos-rebuild switch</ | $ nixos-rebuild switch</syntaxhighlight> | ||
Note: if you're using an ec2 instance based on the HVM virtualization type, you will need to set the | Note: if you're using an ec2 instance based on the HVM virtualization type, you will need to set the | ||
| 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> | ||
<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"> | ||
$ NIXOS=/path/to/nixos NIXPKGS=/path/to/nixpkgs /path/to/nixos/maintainers/scripts/create-amis.sh</ | $ NIXOS=/path/to/nixos NIXPKGS=/path/to/nixpkgs /path/to/nixos/maintainers/scripts/create-amis.sh</syntaxhighlight> | ||
This will build, bundle and upload AMIs to several EC2 regions. Note that the resulting AMIs will be public. | This will build, bundle and upload AMIs to several EC2 regions. Note that the resulting AMIs will be public. | ||
| Line 92: | Line 92: | ||
<li>Build the raw disk image: | <li>Build the raw disk image: | ||
< | <syntaxhighlight lang="bash"> | ||
$ 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 \ | ||
--argstr system i686-linux</ | --argstr system i686-linux</syntaxhighlight> | ||
(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"> | ||
$ ls -l ./result/nixos.img | $ ls -l ./result/nixos.img | ||
-r--r--r-- 1 root nixbld 4294967296 Jan 1 1970 ./result/nixos.img</ | -r--r--r-- 1 root nixbld 4294967296 Jan 1 1970 ./result/nixos.img</syntaxhighlight> | ||
<li>Bundle the image: | <li>Bundle the image: | ||
< | <syntaxhighlight lang="bash"> | ||
$ 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 117: | Line 117: | ||
Digests generated. | Digests generated. | ||
Creating bundle manifest... | Creating bundle manifest... | ||
ec2-bundle-image complete.</ | ec2-bundle-image complete.</syntaxhighlight> | ||
The resulting bundle is stored in <tt>/tmp/nixos.img.manifest.xml</tt> and <tt>/tmp/nixos.img.part.*</tt>. Use <tt>--arch x86_64</tt> for 64-bit images. For the kernel, you should use <tt>pv-grub-hd0_1.02-<em>arch</em></tt> (use <tt>ec2-describe-images -a</tt> to find the AMI ID). This is the PV-GRUB "kernel" that boots the native NixOS kernel in the image. | The resulting bundle is stored in <tt>/tmp/nixos.img.manifest.xml</tt> and <tt>/tmp/nixos.img.part.*</tt>. Use <tt>--arch x86_64</tt> for 64-bit images. For the kernel, you should use <tt>pv-grub-hd0_1.02-<em>arch</em></tt> (use <tt>ec2-describe-images -a</tt> to find the AMI ID). This is the PV-GRUB "kernel" that boots the native NixOS kernel in the image. | ||
| Line 123: | Line 123: | ||
<li>Upload the bundle: | <li>Upload the bundle: | ||
< | <syntaxhighlight lang="bash"> | ||
$ 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 135: | Line 135: | ||
Uploaded manifest. | Uploaded manifest. | ||
Bundle upload completed. | Bundle upload completed. | ||
</ | </syntaxhighlight> | ||
(Of course you can use any other bucket location as well.) | (Of course you can use any other bucket location as well.) | ||
| Line 141: | Line 141: | ||
<li>Register the image as a private AMI: | <li>Register the image as a private AMI: | ||
< | <syntaxhighlight lang="bash"> | ||
$ 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</ | IMAGE ami-efffd59b</syntaxhighlight> | ||
To make it public: | To make it public: | ||
< | <syntaxhighlight lang="bash"> | ||
$ ec2-modify-image-attribute ami-efffd59b -l -a all</ | $ ec2-modify-image-attribute ami-efffd59b -l -a all</syntaxhighlight> | ||
</ol> | </ol> | ||
[[Category:Deployment]] | [[Category:Deployment]] | ||