Install NixOS on Amazon EC2: Difference between revisions

From NixOS Wiki
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:


<pre>$ nix-env -i ec2-api-tools</pre>
<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:


<pre>
<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 ...</pre>
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:


<pre>
<syntaxhighlight lang="bash">
$ ec2-request-spot-instances -t m1.large -k gsg-keypair -p 0.3 ami-ecb49e98</pre>
$ 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>:


<pre>
<syntaxhighlight lang="bash">
$ ssh -i id_rsa-gsg-keypair root@ec2-79-125-97-89.eu-west-1.compute.amazonaws.com</pre>
$ 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):


<pre>
<syntaxhighlight lang="bash">
$ ec2-authorize default -p 22 -s 0.0.0.0/0</pre>
$ 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:


<pre>
<syntaxhighlight lang="bash">
$ nixos-checkout
$ nixos-checkout
$ nixos-rebuild pull</pre>
$ nixos-rebuild pull</syntaxhighlight>


You should now be able to install software, e.g.
You should now be able to install software, e.g.


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


or reconfigure the instance:
or reconfigure the instance:


<pre>
<syntaxhighlight lang="bash">
$ nano /etc/nixos/configuration.nix
$ nano /etc/nixos/configuration.nix
$ nixos-rebuild switch</pre>
$ 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:


<pre>$ nix-env -i ec2-ami-tools</pre>
<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:  


<pre>
<syntaxhighlight lang="bash">
$ NIXOS=/path/to/nixos NIXPKGS=/path/to/nixpkgs /path/to/nixos/maintainers/scripts/create-amis.sh</pre>
$ 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:


<pre>
<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</pre>
     --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>:


<pre>
<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</pre>
-r--r--r-- 1 root nixbld 4294967296 Jan  1  1970 ./result/nixos.img</syntaxhighlight>


<li>Bundle the image:
<li>Bundle the image:


<pre>
<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.</pre>
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:


<pre>
<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.
</pre>
</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:


<pre>
<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</pre>
IMAGE  ami-efffd59b</syntaxhighlight>


To make it public:
To make it public:


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


</ol>
</ol>


[[Category:Deployment]]
[[Category:Deployment]]

Revision as of 19:47, 26 August 2017

This article has some notes on installing NixOS on Amazon's Elastic Compute Cloud (EC2). EC2 support is work in progress.

Below we assume that the following environment variables are set:

  • $EC2_CERT: path to your AWS X.509 certificate (PEM file).
  • $EC2_PRIVATE_KEY: path to the corresponding private key (PEM file).
  • $AWS_ACCOUNT: AWS account number (e.g. 1234-5678-9012).
  • $AWS_ACCESS_KEY_ID: AWS access key ID (e.g. AKIAJM...).
  • $AWS_SECRET_ACCESS_KEY: corresponding secret access key (e.g. klG5...).
  • $AWS_CALLING_FORMAT may need to be set to SUBDOMAIN.
  • $EC2_URL should be set to the desired region, e.g. https://ec2.eu-west-1.amazonaws.com/.

Public NixOS AMIs

The list of current NixOS AMI's are available at https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/ec2-amis.nix

Running NixOS instances

  1. To create instances from the command line, you need to install the EC2 API tools:
    $ nix-env -i ec2-api-tools
    

    Alternatively, you can create NixOS instances using the AWS Management Console.

  2. Start a small instance running NixOS:
    $ ec2-run-instances -k gsg-keypair ami-c9f2d8bd
    RESERVATION  r-10ca4167  516444698777  default
    INSTANCE     i-f6d3b781  ami-c9f2d8bd  pending gsg-keypair  0 m1.small ...
    

    gsg-keypair denotes an SSH key pair created with ec2-add-keypair. To run a 64-bit instance, you must specify a 64-bit-capable instance type, e.g., -t m1.large. If your requirements allow it, you may prefer using a cheap spot instance:

    $ ec2-request-spot-instances -t m1.large -k gsg-keypair -p 0.3 ami-ecb49e98
    
  3. After a while the instance should be running, and you can log in using the SSH key generated by ec2-add-keypair:
    $ ssh -i id_rsa-gsg-keypair root@ec2-79-125-97-89.eu-west-1.compute.amazonaws.com
    

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

    $ ec2-authorize default -p 22 -s 0.0.0.0/0
    
  4. To start working with the instance, you may want to do the following to obtain the Nixpkgs and NixOS sources:
    $ nixos-checkout
    $ nixos-rebuild pull
    

    You should now be able to install software, e.g.

    $ nix-env -i emacs
    

    or reconfigure the instance:

    $ nano /etc/nixos/configuration.nix
    $ nixos-rebuild switch
    

    Note: if you're using an ec2 instance based on the HVM virtualization type, you will need to set the

    ec2.hvm = true;
    

    setting in configuration.nix; otherwise GRUB won't pick up changes to your configuration across reboots.

Creating a NixOS AMI

The following steps show how to build your own NixOS Amazon Image (AMI), upload it to S3, and start an instance.

  1. Install the EC2 AMI tools:
    $ nix-env -i ec2-ami-tools
    
  2. The easy way to build NixOS AMIs is to run the following command:
    $ NIXOS=/path/to/nixos NIXPKGS=/path/to/nixpkgs /path/to/nixos/maintainers/scripts/create-amis.sh
    

    This will build, bundle and upload AMIs to several EC2 regions. Note that the resulting AMIs will be public.

    Alternatively, you can perform the steps below to create the AMI manually.

  3. Build the raw disk image:
    $ NIXPKGS=/path/to/nixpkgs NIXOS_CONFIG=/path/to/nixos/modules/virtualisation/amazon-config.nix \
        nix-build /path/to/nixos -A config.system.build.amazonImage \
        --argstr system i686-linux
    

    (Use --argstr system x86_64-linux to build a 64-bit image.) This will produce an ext3 disk image in ./result/nixos.img:

    $ ls -l ./result/nixos.img 
    -r--r--r-- 1 root nixbld 4294967296 Jan  1  1970 ./result/nixos.img
    
  4. Bundle the image:
    $ ec2-bundle-image -i ./result/nixos.img --user $AWS_ACCOUNT --arch i386 \
        -c $EC2_CERT -k $EC2_PRIVATE_KEY \
        --kernel aki-c34d67b7
    Bundling image file...
    Splitting /tmp/nixos.img.tar.gz.enc...
    Created nixos.img.part.00
    ...
    Created nixos.img.part.19
    Generating digests for each part...
    Digests generated.
    Creating bundle manifest...
    ec2-bundle-image complete.
    

    The resulting bundle is stored in /tmp/nixos.img.manifest.xml and /tmp/nixos.img.part.*. Use --arch x86_64 for 64-bit images. For the kernel, you should use pv-grub-hd0_1.02-arch (use ec2-describe-images -a to find the AMI ID). This is the PV-GRUB "kernel" that boots the native NixOS kernel in the image.

  5. Upload the bundle:
    $ ec2-upload-bundle -b nixos-img -m /tmp/nixos.img.manifest.xml \
        -a $AWS_ACCESS_KEY_ID -s $AWS_SECRET_ACCESS_KEY --location EU
    Creating bucket...
    Uploading bundled image parts to the S3 bucket nixos-img ...
    Uploaded nixos.img.part.00
    Uploaded nixos.img.part.01
    ...
    Uploaded nixos.img.part.19
    Uploading manifest ...
    Uploaded manifest.
    Bundle upload completed.
    

    (Of course you can use any other bucket location as well.)

  6. Register the image as a private AMI:
    $ ec2-register nixos-img/nixos.img.manifest.xml -n nixos-img -d '...description...'
    IMAGE   ami-efffd59b
    

    To make it public:

    $ ec2-modify-image-attribute ami-efffd59b -l -a all