nixos-and-flakes-book/docs/nixos-with-flakes/get-started-with-nixos.md

71 lines
4.1 KiB
Markdown
Raw Normal View History

2023-06-30 09:00:03 +00:00
# Get Started with NixOS
2023-07-04 04:18:46 +00:00
Now that we have learned the basics of the Nix language, we can start using it to configure our NixOS system. The default configuration file for NixOS is located at `/etc/nixos/configuration.nix`. This file contains all the declarative configuration for the system, including settings for the time zone, language, keyboard layout, network, users, file system, and boot options.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
To modify the system state in a reproducible manner (which is highly recommended), we need to manually edit the `/etc/nixos/configuration.nix` file and then execute `sudo nixos-rebuild switch` to apply the modified configuration. This command generates a new system environment based on the modified configuration file, sets the new environment as the default one, and preserves the previous environment in the boot options of grub/systemd-boot. This ensures that we can always roll back to the old environment if the new one fails to start.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
While `/etc/nixos/configuration.nix` is the classic method for configuring NixOS, it relies on data sources configured by `nix-channel` and lacks a version-locking mechanism, making it challenging to ensure the reproducibility of the system. A better approach is to use Flakes, which provides reproducibility and facilitates configuration management.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
In this section, we will first learn how to manage NixOS using the classic method (`/etc/nixos/configuration.nix`), and then we will explore the more advanced Flakes.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
## Configuring the System using `/etc/nixos/configuration.nix`
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
The `/etc/nixos/configuration.nix` file is the default and classic method for configuring NixOS. While it lacks some of the advanced features of Flakes, it is still widely used and provides flexibility in system configuration.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
To illustrate how to use `/etc/nixos/configuration.nix`, let's consider an example where we enable SSH and add a user named `ryan` to the system. We can achieve this by adding the following content to `/etc/nixos/configuration.nix`:
2023-06-23 12:29:12 +00:00
```nix
{ config, pkgs, ... }:
{
imports =
[ # Include the results of the hardware scan.
./hardware-configuration.nix
];
2023-07-04 04:18:46 +00:00
# Omit previous configuration settings...
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
# Add user 'ryan'
2023-06-23 12:29:12 +00:00
users.users.ryan = {
isNormalUser = true;
description = "ryan";
extraGroups = [ "networkmanager" "wheel" ];
openssh.authorizedKeys.keys = [
2023-07-04 04:18:46 +00:00
# Replace with your own public key
2023-06-23 12:29:12 +00:00
"ssh-ed25519 <some-public-key> ryan@ryan-pc"
];
packages = with pkgs; [
firefox
# thunderbird
];
};
2023-07-04 04:18:46 +00:00
# Enable openssh-server
2023-06-23 12:29:12 +00:00
services.openssh = {
enable = true;
2023-07-04 04:18:46 +00:00
permitRootLogin = "no"; # Disable root login
passwordAuthentication = false; # Disable password login
2023-06-23 12:29:12 +00:00
openFirewall = true;
2023-07-04 04:18:46 +00:00
forwardX11 = true; # Enable X11 forwarding
2023-06-23 12:29:12 +00:00
};
2023-07-04 04:18:46 +00:00
# Omit the rest of the configuration...
2023-06-23 12:29:12 +00:00
}
```
2023-07-04 04:18:46 +00:00
In this configuration, we declare our intention to enable the openssh service, add an SSH public key for the user 'ryan', and disable password login.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
To deploy the modified configuration, run `sudo nixos-rebuild switch`. This command will apply the changes, generate a new system environment, and set it as the default. You can now log in to the system using SSH with the configured SSH keys.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
Remember that any reproducible changes to the system can be made by modifying the `/etc/nixos/configuration.nix` file and deploying the changes with `sudo nixos-rebuild switch`.
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
To find configuration options and documentation:
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
- Use search engines like Google, e.g., search for `Chrome NixOS` to find NixOS-related information about Chrome. The NixOS Wiki and the source code of Nixpkgs are usually among the top results.
- Utilize the [NixOS Options Search](https://search.nixos.org/options) to search for keywords.
- Refer to the [Configuration section](https://nixos.org/manual/nixos/unstable/index.html#ch-configuration) in the NixOS Manual for system-level configuration documentation.
- Search for keywords directly in the source code of [nixpkgs](https://github.com/NixOS/nixpkgs) on GitHub.
2023-06-23 12:29:12 +00:00
## References
2023-07-04 04:18:46 +00:00
- [Overview of the NixOS Linux distribution](https://nixos.wiki/wiki/Overview_of_the_NixOS_Linux_distribution)