Skip to content

Commit

Permalink
better examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brainrake committed Feb 27, 2019
1 parent cfa12e4 commit 67c0364
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 38 deletions.
68 changes: 36 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ check the [cheatsheet](cheatsheet.md)
## outline

- setup (5m)
- package management with `nix-env` (10m)
- isolated environments with `nix-shell` (10m)
- declarative operating system configuration with NixOS
- `nix-env` package management (10m)
- `nix-shell` isolated environments (10m)
- NixOS declarative operating system configuration
- introduction (5m)
- service (5m)
- network (5m)
Expand All @@ -17,70 +17,74 @@ check the [cheatsheet](cheatsheet.md)

- [download and install VirtualBox](https://www.virtualbox.org/wiki/Downloads)
- [download NixOS VirtualBox appliance](https://nixos.org/nixos/download.html), double click, launch VM
- login with `demo`/`demo` and run these commands:

```
sudo nix-channel --add https://nixos.org/channels/nixos-unstable
sudo nix-channel --update
sudo nixos-rebuild switch
```
- log in with `demo`/`demo`
- open a terminal, eg. on the bottom left corner of the screen, click the Launcher button → Applications → System → Terminal


## package management with `nix-env`

| | |
|---|---|
| update package list | `sudo nix-channel --update` |
| search | `nix search vim` |
| live search | [nixos.org/nixos/packages.html](https://nixos.org/nixos/packages.html) |
| install | `nix-env -i vim` |
| uninstall | `nix-env -e vim` |
| rollback | `nix-env --rollback` |
| | | |
|---|---|---|
| update package list | `sudo nix-channel --update` ||
| search | `nix search hello` ||
| live search | [nixos.org/nixos/packages.html](https://nixos.org/nixos/packages.html) ||
| install | `nix-env -i hello` | run with `hello` |
| uninstall | `nix-env -e hello` | running `hello` fails |
| rollback | `nix-env --rollback` | `hello` is back |


## isolated environments with `nix-shell`

- start a shell in an env with some packages available
- `nix-shell -p python27Packages.numpy`
- `nix-shell -p python35Packages.numpy`
- `nix-shell -p hello --run 'hello -t'`
- `nix-shell` on its own will load `default.nix` or `shell.nix` from the current directory
- start a shell in an environment with some packages available:
- `nix-shell -p toilet`
- `toilet hello`
- `Ctrl+D` to exit nix-shell
- `toilet hello`
- `nix-shell -p toilet --run 'toilet --gay hello`
- [python example](python.md)
- `nix-shell` on its own will load `default.nix` or `shell.nix` from the current directory, where you can define an environment for a project


## declarative configuration management with NixOS
## NixOS declarative operating system configuration

- edit `/etc/nixos/configuration.nix`
- eg. add `programs.bash.enableCompletion = true;` before the last `}`
- eg. add `services.openssh.enable = true;` before the last `}`
- `nixos-rebuild switch` to the new configuration
- see option's current value and documentation by running `nixos-option programs.bash.enableCompletion`
- search [nixos.org/nixos/options.html](https://nixos.org/nixos/options.html)
- see option's current value and documentation by running `nixos-option services.openssh.enable`
- search [nixos.org/nixos/options.html](https://nixos.org/nixos/options.html), click the result, click the link after "Declared in:"
- `nixos-rebuild switch --rollback` to previous configuration

let's try a few configuration options:


### service

- `services.sshd.enable = true;`
- `services.openssh.enable = true;`
- run `ssh demo@localhost` on the vm and log in with `demo` / `demo`
- [nginx](nixos/nginx.nix)
- go to http://localhost/status on the vm
- [monit](nixos/monit.nix)
- go to http://localhost:2812 on the vm, log in with `admin` / `monit`


### system

- `system.autoUpgrade.enable = true;`
- don't forget to check the documentation [nixos.org/nixos/options.html](https://nixos.org/nixos/options.html)
- `environment.systemPackages = [ pkgs.vim pkgs.cmatrix ];`
- all users can run `cmatrix`


### user

- [staruser](nixos/staruser.nix) normal user
- [staruser-sudo](nixos/staruser-sudo.nix) with sudo
- [staruser-sshkey](nixos/staruser-sshkey.nix) with ssh public key authentication
- [user.nix](nixos/user.nix) normal user
- [user-sudo.nix](nixos/user-sudo.nix) with sudo
- [user-sshkey.nix](nixos/user-sshkey.nix) with ssh public key authentication


### network

- `networking.hostname = "star-darab";`
- `networking.hostname = "darkstar";`
- `networking.firewall.allowedTCPPorts = [ 22 80 8000 ];`


Expand Down
3 changes: 3 additions & 0 deletions nixos/openssh.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
services.openssh.enable = true;
}
3 changes: 0 additions & 3 deletions nixos/sshd.nix

This file was deleted.

2 changes: 1 addition & 1 deletion nixos/staruser-sshkey.nix → nixos/user-sshkey.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
users.users.staruser = {
users.users.myuser = {
isNormalUser = true;
openssh.authorizedKeys.keys = [ "... ssh public key here ..." ];
};
Expand Down
2 changes: 1 addition & 1 deletion nixos/staruser-sudo.nix → nixos/user-sudo.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
security.sudo.enable = true;
users.users.staruser = {
users.users.user = {
isNormalUser = true;
password = "changeme";
extraGroups = [ "wheel" ];
Expand Down
2 changes: 1 addition & 1 deletion nixos/staruser.nix → nixos/user.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
users.users.staruser = {
users.users.myuser = {
isNormalUser = true;
password = "changeme";
};
Expand Down
9 changes: 9 additions & 0 deletions python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Start a shell with a versions of python and python packages available:
- `nix-shell -p python27Packages.numpy`
- run `python --version`
- run `python`
- type `import numpy; numpy.__version__`
- `Ctrl+D` to exit python
- `Ctrl+D` to exit nix-shell
- `nix-shell -p python36Packages.numpy`
- as above

0 comments on commit 67c0364

Please sign in to comment.