Install NixOS on GCE: Difference between revisions
imported>Jonas No edit summary |
imported>Korfuri Update instructions for GCE NixOS setup, reflecting the lack of updates since 20.09. Focus on the only current option, build-your-own-image. |
||
Line 1: | Line 1: | ||
This is a recipe for creating a NixOS machine on Google Compute Engine (GCE) which is part of [https://cloud.google.com/ Google Cloud Platform]. | This is a recipe for creating a NixOS machine on Google Compute Engine (GCE) which is part of [https://cloud.google.com/ Google Cloud Platform]. | ||
This tutorial assumes you have already set up and account and project under Google Cloud Platform. | This tutorial assumes you have already set up and account and project under Google Cloud Platform. | ||
There are no publicly provided images of recent releases of NixOS. There are some old releases at [https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/gce-images.nix <nixpkgs/nixos/modules/virtualisation/gce-images.nix>] and in the <code>gs://nixos-images</code> and <code>gs://nixos-cloud-images</code> public buckets, but these have not been updated in years. Instead, it is recommended you build your own image. | |||
== Bootstrapping a NixOS image from the build of your choice == | |||
This assumes you have created a Google Cloud project and a Google Cloud Storage bucket in that project. Set them as variables: | |||
< | <syntaxhighlight lang="bash"> | ||
PROJECT_ID=my-project-id | |||
BUCKET_NAME=my-bucket-name # Set the bucket name without the gs:// prefix | |||
</syntaxhighlight> | |||
You'll need <code>gsutil</code> installed. See the [https://cloud.google.com/sdk/docs/install-sdk Google Cloud SDK documentation] for full instructions, or simply use: | |||
= | <syntaxhighlight lang="bash"> | ||
$ nix-shell -p google-cloud-sdk | |||
$ gcloud auth login | |||
[ ... this opens a webpage to authenticate your gcloud SDK, follow the authentication prompt in your browser] | |||
$ gcloud gcloud config set project $PROJECT_ID | |||
</syntaxhighlight> | |||
Prepare a local copy of the nixpkgs repository in the state you want to build from. If you want to build a released version, this means checking out one of the release branches from the nixpkgs repository. Make sure you haven't left any unwanted local changes in it. These examples assume you've checked it out at <code>./nixpkgs</code>. | |||
<syntaxhighlight lang=" | <syntaxhighlight lang="bash"> | ||
$ | $ BUCKET_NAME=my_bucket_name nixpkgs/nixos/maintainers/scripts/gce/create-gce.sh | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This will create an image and upload it to the bucket. It will also create a GCE image that VMs can use. | |||
Note: If you build an image from a commit later then [https://github.com/NixOS/nixpkgs/commit/b894dd8b821d74b25911f63762c24024107d9372 this one], you will need to add <code>enable-oslogin = "TRUE"</code> to the instance metadata, to be able to login. | |||
== Create a VM instance == | == Create a VM instance == | ||
#In [https://console.cloud.google.com/compute/instances console | #In [https://console.cloud.google.com/compute/instances the GCE console], select <code>CREATE INSTANCE</code> | ||
## <b>Boot disk</b> : <i>Custom images</i> | ## <b>Boot disk</b> : <i>Change</i>, then <i>Custom images</i> | ||
### <b>Image</b> : | ### <b>Image</b> : pick the image recently created | ||
## | ## You do not need to add SSH keys, NixOS is set up for [https://cloud.google.com/compute/docs/instances/managing-instance-access Google OS Login] | ||
## <b>Metadata</b> | ## <b>Metadata</b> | ||
### <b>key</b> : <i>enable-oslogin</i> | ### <b>key</b> : <i>enable-oslogin</i> | ||
Line 84: | Line 75: | ||
At this point you may want to snapshot this image and use this snapshot to make future VMs. | At this point you may want to snapshot this image and use this snapshot to make future VMs. | ||
[[Category:Server]] | [[Category:Server]] |
Revision as of 15:07, 11 July 2022
This is a recipe for creating a NixOS machine on Google Compute Engine (GCE) which is part of Google Cloud Platform.
This tutorial assumes you have already set up and account and project under Google Cloud Platform.
There are no publicly provided images of recent releases of NixOS. There are some old releases at <nixpkgs/nixos/modules/virtualisation/gce-images.nix> and in the gs://nixos-images
and gs://nixos-cloud-images
public buckets, but these have not been updated in years. Instead, it is recommended you build your own image.
Bootstrapping a NixOS image from the build of your choice
This assumes you have created a Google Cloud project and a Google Cloud Storage bucket in that project. Set them as variables:
PROJECT_ID=my-project-id
BUCKET_NAME=my-bucket-name # Set the bucket name without the gs:// prefix
You'll need gsutil
installed. See the Google Cloud SDK documentation for full instructions, or simply use:
$ nix-shell -p google-cloud-sdk
$ gcloud auth login
[ ... this opens a webpage to authenticate your gcloud SDK, follow the authentication prompt in your browser]
$ gcloud gcloud config set project $PROJECT_ID
Prepare a local copy of the nixpkgs repository in the state you want to build from. If you want to build a released version, this means checking out one of the release branches from the nixpkgs repository. Make sure you haven't left any unwanted local changes in it. These examples assume you've checked it out at ./nixpkgs
.
$ BUCKET_NAME=my_bucket_name nixpkgs/nixos/maintainers/scripts/gce/create-gce.sh
This will create an image and upload it to the bucket. It will also create a GCE image that VMs can use.
Note: If you build an image from a commit later then this one, you will need to add enable-oslogin = "TRUE"
to the instance metadata, to be able to login.
Create a VM instance
- In the GCE console, select
CREATE INSTANCE
- Boot disk : Change, then Custom images
- Image : pick the image recently created
- You do not need to add SSH keys, NixOS is set up for Google OS Login
- Metadata
- key : enable-oslogin
- value : TRUE
- Boot disk : Change, then Custom images
- Click Create
- Wait until your VM instance is ready
- Under Connect, click SSH
Optional: add user account
Once you are logged into your NixOS machine, you can create a user account for yourself with administrator privileges:
1. chmod u+w /etc/nixos/configuration.nix
2. nano -w /etc/nixos/configuration.nix
3. Add the following to the configuration:
security.sudo.wheelNeedsPassword = false;
users.extraUsers.<your-username> = {
createHome = true;
home = "/home/<your-username>";
description = "<your-name>";
group = "users";
extraGroups = [ "wheel" ];
useDefaultShell = true;
openssh.authorizedKeys.keys = [ "<contents of your ~/.ssh/id_rsa.pub>" ];
};
4. Save this file and run nixos-rebuild switch --upgrade
5. Reboot and log back in with your user account
Snapshots
At this point you may want to snapshot this image and use this snapshot to make future VMs.