diff --git a/Makefile b/Makefile index a8155f6289..f36f4a738f 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ HTML = index.html download.html news.html learn.html community.html \ teams/rfc-steering-committee.html teams/security.html teams/marketing.html \ teams/nixos_release.html teams/infrastructure.html teams/nixcon.html \ teams/discourse.html \ - guides/contributing.html guides/quickstart.html \ + guides/contributing.html guides/install-nix.html guides/first-shell.html guides/dev-environment.html guides/docker-image.html \ nix/index.html nix/about.html \ nixpkgs/index.html \ nixos/index.html nixos/about.html \ diff --git a/guides/dev-environment.tt b/guides/dev-environment.tt new file mode 100644 index 0000000000..1b13190cbe --- /dev/null +++ b/guides/dev-environment.tt @@ -0,0 +1,71 @@ +[% WRAPPER layout.tt hideTitle=1 title="Guide: Install Nix" menu='nixos' %] + +

Setup a development environment

+ +

As an exercise, let us build a Python web application using the Flask web framework.

+ +

Create a new file default.nix. This file is conventionally used for specifying packages:

+ +
+{ pkgs ? import <nixpkgs> {} }:
+
+pkgs.python3Packages.buildPythonApplication {
+  pname = "myapp";
+  src = ./.;
+  version = "0.1";
+  propagatedBuildInputs = [ pkgs.python3Packages.flask ];
+}
+
+ +

You will also need a simple Flask app as main.py:

+ +
+#! /usr/bin/env python
+
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route("/")
+def hello():
+    return "Hello, Nix!"
+
+def run():
+    app.run(host='0.0.0.0')
+
+ +

and a setup.py script:

+ +
+from setuptools import setup
+
+setup(
+    name='myapp',
+    version='0.1',
+    py_modules=['myapp'],
+    entry_points={
+        'console_scripts': ['myapp = myapp:run']
+    },
+)
+
+ +

Now build the package with:

+ +
+nix-build
+
+ +

This will create a symbolic link result to our package's path in the Nix store, which looks like /nix/store/6i4l781jwk5vbia8as32637207kgkllj-myapp-0.1. Look around to see what is inside.

+ +

You may notice we can run the application from the package like ./result/bin/main.py. We can still use the default.nix as a shell environment to get the same result:

+ +
+nix-shell default.nix
+python3 main.py
+
+ +

In this context, Nix takes on the role that you would otherwise use pip or virtualenv for. Nix installs required dependencies and separates the environment from others on your system.

+ +

You can check this Nix configuration into version control and share it with others to make sure you are all running the same software. Especially with many dependencies this is a great way to prevent configuration drift between different team members & contributors.

+ +[% END %] diff --git a/guides/docker-image.tt b/guides/docker-image.tt new file mode 100644 index 0000000000..a630e55371 --- /dev/null +++ b/guides/docker-image.tt @@ -0,0 +1,47 @@ +[% WRAPPER layout.tt hideTitle=1 title="Guide: Install Nix" menu='nixos' %] + +

Build a Docker image

+ +

For this section we assume you have Docker installed.

+ +

When you set up the development environment, you created a package of a Flask app. Now you may want to deploy it. One way to do this is to package it as a Docker container that can be hosted on a container hosting platform.

+ +

Nix comes with tools for building Docker containers. To turn our Python app into a Docker container, we only have to specify what command to run. Nix will take care of building a minimal image required to make that happen.

+ +

Create a file docker.nix:

+ +
+{ pkgs ? import <nixpkgs> {} }:
+
+let
+  app = pkgs.callPackage ./default.nix {
+    inherit pkgs;
+  };
+
+in pkgs.dockerTools.buildImage {
+  name = "myapp";
+  tag = "latest";
+  config = {
+    Cmd = [ "${app}/bin/myapp" ];
+  };
+}
+
+ +

We are calling the expression under ./default.nix and pass it our own pkgs. Under Cmd we use the variable appto retrieve the path to the executable Python app.

+ +

Now we can build the Docker image and load it into Docker:

+ +
+nix-build docker.nix
+docker load --input result
+
+ +

Now Docker has an image tagged myapp:latest. Run the Docker image and make sure to forward port 5000, so you can access the app in the browser:

+ +
+docker run -it -p 5000:5000 myapp:latest
+
+ +

Now you can see the site on http://localhost:5000.

+ +[% END %] diff --git a/guides/first-shell.tt b/guides/first-shell.tt new file mode 100644 index 0000000000..4fc78afc88 --- /dev/null +++ b/guides/first-shell.tt @@ -0,0 +1,47 @@ +[% WRAPPER layout.tt hideTitle=1 title="Guide: Install Nix" menu='nixos' %] + +

Your first shell

+ +

Nix can create reproducible environments given a declarative configuration called a Nix expression. Reproducible means you can share the configuration with others and guarantee that they are using the same software as you.

+ +

To get started, make a new folder and create a file called shell.nix with the following contents:

+ +
+{ pkgs ? import <nixpkgs> {} }:
+
+pkgs.mkShell {
+  buildInputs = [
+    pkgs.which
+    pkgs.htop
+  ];
+}
+
+ +

Basically we import our package channel nixpkgs and make a shell with which and htop as inputs. To enter this environment, type in:

+ +
+nix-shell
+
+ +

The command will start downloading the missing packages from the cache. This may take a few moments. When it is done, you are dropped into a new shell. This shell provides the packages specified in shell.nix.

+ +

Run htop to confirm it is present. Quit the program again by hitting Q.

+ +

Now try which htop to check where the htop command is on-disk. You should see something similar to this:

+ +
+/nix/store/y3w2i8kfdbfj9rx287ad52rahjpgv423-htop-2.2.0/bin/htop
+
+ +

This is the path to the binary in the Nix store. Nix installs all packages into the store using a combination of its hash, name and version.

+ +

You can search for available packages using nix-env -qa, for example:

+ +
+nix-env -qa python3
+nix-env -qa nodejs
+nix-env -qa ghc
+nix-env -qa cargo
+
+ +[% END %] diff --git a/guides/install-nix.tt b/guides/install-nix.tt new file mode 100644 index 0000000000..ba8754aaab --- /dev/null +++ b/guides/install-nix.tt @@ -0,0 +1,21 @@ +[% WRAPPER layout.tt hideTitle=1 title="Guide: Install Nix" menu='nixos' %] + +

Install Nix

+ +

While NixOS is a Linux distribution based on Nix, you can install Nix on other Linux distributions, MacOS and Windows via WSL using the install script from our website:

+ +
+curl -L https://nixos.org/nix/install | sh
+
+ +

(For security, you may want to verify the script using our GPG signatures.)

+ +

Check that the installation was successful by running

+ +
+nix-channel --list
+
+ +

This command displays the package distribution channel used by Nix. By default, this is https://nixos.org/channels/nixpkgs-unstable

+ +[% END %] diff --git a/guides/quickstart.tt b/guides/quickstart.tt deleted file mode 100644 index cca8a50984..0000000000 --- a/guides/quickstart.tt +++ /dev/null @@ -1,194 +0,0 @@ -[% WRAPPER layout.tt hideTitle=1 title="Quickstart guide" menu='nixos' %] - -

Quickstart guide

- -

This quickstart guide will get you started with the Nix package manager.

- -

You will learn how to…

- - - - -

Install Nix

- -

While NixOS is a Linux distribution based on Nix, you can install Nix on other Linux distributions, MacOS and Windows via WSL using the install script from our website:

- -
-curl -L https://nixos.org/nix/install | sh
-
- -

(For security, you may want to verify the script using our GPG signatures.)

- -

Check that the installation was successful by running

- -
-nix-channel --list
-
- -

This command displays the package distribution channel used by Nix. By default, this is https://nixos.org/channels/nixpkgs-unstable

- - -

Your first shell

- -

Nix can create reproducible environments given a declarative configuration called a Nix expression. Reproducible means you can share the configuration with others and guarantee that they are using the same software as you.

- -

To get started, make a new folder and create a file called shell.nix with the following contents:

- -
-{ pkgs ? import <nixpkgs> {} }:
-
-pkgs.mkShell {
-  buildInputs = [
-    pkgs.which
-    pkgs.htop
-  ];
-}
-
- -

Basically we import our package channel nixpkgs and make a shell with which and htop as inputs. To enter this environment, type in:

- -
-nix-shell
-
- -

The command will start downloading the missing packages from the cache. This may take a few moments. When it is done, you are dropped into a new shell. This shell provides the packages specified in shell.nix.

- -

Run htop to confirm it is present. Quit the program again by hitting Q.

- -

Now try which htop to check where the htop command is on-disk. You should see something similar to this:

- -
-/nix/store/y3w2i8kfdbfj9rx287ad52rahjpgv423-htop-2.2.0/bin/htop
-
- -

This is the path to the binary in the Nix store. Nix installs all packages into the store using a combination of its hash, name and version.

- -

You can search for available packages using nix-env -qa, for example:

- -
-nix-env -qa python3
-nix-env -qa nodejs
-nix-env -qa ghc
-nix-env -qa cargo
-
- - -

Setup a development environment

- -

As an exercise, let us build a Python web application using the Flask web framework.

- -

Create a new file default.nix. This file is conventionally used for specifying packages:

- -
-{ pkgs ? import <nixpkgs> {} }:
-
-pkgs.python3Packages.buildPythonApplication {
-  pname = "myapp";
-  src = ./.;
-  version = "0.1";
-  propagatedBuildInputs = [ pkgs.python3Packages.flask ];
-}
-
- -

You will also need a simple Flask app as main.py:

- -
-#! /usr/bin/env python
-
-from flask import Flask
-
-app = Flask(__name__)
-
-@app.route("/")
-def hello():
-    return "Hello, Nix!"
-
-def run():
-    app.run(host='0.0.0.0')
-
- -

and a setup.py script:

- -
-from setuptools import setup
-
-setup(
-    name='myapp',
-    version='0.1',
-    py_modules=['myapp'],
-    entry_points={
-        'console_scripts': ['myapp = myapp:run']
-    },
-)
-
- -

Now build the package with:

- -
-nix-build
-
- -

This will create a symbolic link result to our package's path in the Nix store, which looks like /nix/store/6i4l781jwk5vbia8as32637207kgkllj-myapp-0.1. Look around to see what is inside.

- -

You may notice we can run the application from the package like ./result/bin/main.py. We can still use the default.nix as a shell environment to get the same result:

- -
-nix-shell default.nix
-python3 main.py
-
- -

In this context, Nix takes on the role that you would otherwise use pip or virtualenv for. Nix installs required dependencies and separates the environment from others on your system.

- -

You can check this Nix configuration into version control and share it with others to make sure you are all running the same software. Especially with many dependencies this is a great way to prevent configuration drift between different team members & contributors.

- - -

Build a Docker image

- -

For this section we assume you have Docker installed.

- -

Now that you have package a Flask app, you may want to deploy it. One way to do this is to package it as a Docker container that can be hosted on a container hosting platform.

- -

Nix comes with tools for building Docker containers. To turn our Python app into a Docker container, we only have to specify what command to run. Nix will take care of building a minimal image required to make that happen.

- -

Create a file docker.nix:

- -
-{ pkgs ? import <nixpkgs> {} }:
-
-let
-  app = pkgs.callPackage ./default.nix {
-    inherit pkgs;
-  };
-
-in pkgs.dockerTools.buildImage {
-  name = "myapp";
-  tag = "latest";
-  config = {
-    Cmd = [ "${app}/bin/myapp" ];
-  };
-}
-
- -

We are calling the expression under ./default.nix and pass it our own pkgs. Under Cmd we use the variable appto retrieve the path to the executable Python app.

- -

Now we can build the Docker image and load it into Docker:

- -
-nix-build docker.nix
-docker load --input result
-
- -

Now Docker has an image tagged myapp:latest. Run the Docker image and make sure to forward port 5000, so you can access the app in the browser:

- -
-docker run -it -p 5000:5000 myapp:latest
-
- -

Now you can see the site on http://localhost:5000.

- -[% END %] diff --git a/learn.tt b/learn.tt index c1eb433005..82166b9b9d 100644 --- a/learn.tt +++ b/learn.tt @@ -3,7 +3,10 @@

Guides