presentation-nix-flakes/prezentace.md
2025-03-14 13:42:12 +01:00

395 lines
6.5 KiB
Markdown

---
marp: true
class: invert
paginate: true
footer: "Albert Vala, Filip Kohout, Nix flakes & Nix command"
---
<style>
@import url('https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap');
:root {
background-color: rgb(10,10,10);
font-family: Ubuntu, Arial
}
h1 {
color: rgb(87, 175, 246)
}
code {
background-color: rgb(10,10,10)
}
header {
color: rgb(100,100,100)
}
footer {
color: rgb(100,100,100)
}
</style>
# Nix flakes & Nix command
---
# Nix Command
* Nové nix cli
* Flake support
---
# Porovnání
```shell
nix-shell -p cowsay #Old
nix shell nixpkgs#cowsay #New
nix run nixpkgs#cowsay #New
```
---
# Commandy
| Command | Na |
| ------------ | ---------------------------------- |
| flake new | Vytvoření flaky z template |
| develop | Build environment shell |
| shell | temp shell s nějakým programem |
| config check | Check configu na potenciální chyby |
| run | Spustí output z flaky |
---
# Flakes
* ## Proč?
* Lockfiles.
* Flake inputs
* ## Na co?
* Cokoliv.
---
# Nix command + flakes
- temp shell
```shell
nix shell nixpkgs#alejandra
nix shell github:kamadorueda/alejandra
```
- Show output attr
```shell
nix flake show github:kamadorueda/alejandra
```
---
# Jak použít flakes na systémovou configuraci?
* Flakes používáme jen na management input a output atributů
* Zbytek konfigurace používá inputy definované ve flake.nix
---
```nix
# flake.nix
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
zen-browser.url = "github:0xc000022070/zen-browser-flake";
};
outputs = inputs @ {
nixpkgs,
home-manager,
zen-browser,
...
}: {
nixosConfigurations = {
nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = false;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {inherit zen-browser;};
home-manager.users.nixFreak = import ./home.nix;
}
];
};
};
};
}
```
---
```nix
# home.nix
{
zen-browser,
config,
pkgs,
...
}: {
home.username = "nixFreak";
home.homeDirectory = "/home/nixFreak";
home.stateVersion = "24.05";
home.packages = with pkgs;
[
cowsay
]
++ [
zen-browser.packages."${system}".default
];
programs.home-manager.enable = true;
}
```
---
```nix
# configuration.nix
{
config,
lib,
pkgs,
...
}: {
imports = [
# Include the results of the hardware scan.
./hardware-configuration.nix
];
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
users.users.nixFreak = {
isNormalUser = true;
extraGroups = ["wheel"];
};
system.stateVersion = "25.05";
}
```
---
# Inputs
* `nix flake metadata --json | jq` - zobrazí vstupy
* lock aktuální verze
* `nix flake update` - aktualizace všech vstupů
* `nix flake update [home-manager]` - aktualizace jednoho vstupu
```nix
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-24.11";
nixpkgs-current = "github:nixos/nixpkgs?ref=3058cf84bce1aba7b1820cb24319a171572217ba-dirty";
}
```
---
# Nixos-rebuild
* Je to jen bash script
```shell
nixos-rebuild --flake
nh os switch
```
```shell
nix build /etc/nixos#nixosConfigurations.nixos.config.system.build.toplevel
./result/bin/switch-to-configuration switch
```
---
# Remote building
* `nixos-rebuild --target-host nixFreak@192.168.8.32 switch --flake .#default`
* `--use-remote-sudo`
* `--build-host` - konfigurace tady
* `--target-host` - konfigurace tam
* `nix.settings.trusted-users`
---
# Virtual machine
* `nix run github:nix-community/nixos-generators -- -c ./flake.nix --flake '#default' -f vm --disk-size 20480`
* možnost generovat pro různé platformy (Amazon E2C, Docker, ISO, Proxmox, VMware, vagrant, qcow, ...)
* [https://github.com/nix-community/nixos-generators](https://github.com/nix-community/nixos-generators)
---
# Nix language
* `nix-repl` nebo `nix-instantiate --eval foo.nix`
```nix
# Deklarace setu
foo = {
bar = "test"
}
# "Čtení" setu (evaluation)
foo.bar
# Let-In syntaxe
let
foo = {
bar = "test"
};
in
foo.bar
```
---
```nix
# Rekurze
foo = rec {
bar = 10;
ham = 5;
eggs = bar + ham;
}
```
```nix
# Použití with
environment.systemPackages = with pkgs; [
neovim
wireshark
];
```
---
## Funkce
```nix
# Jednoduchá funkce
let
bar = eggs: eggs + 1;
in
bar 3 # -> 4
# Funkce s více parametry
let
bar = eggs: bacon: (eggs+1) * bacon;
in
bar 3 7 # -> 28
```
---
```nix
let
bar = { bacon, eggs }: bacon + eggs;
in
bar { bacon = 10; eggs = 15; }
```
* konfigurační soubory jsou funkce
```nix
# configuration.nix
{inputs, pkgs}:
{
environment.systemPackages = [
inputs.zen-browser.packages.${system}.default
pkgs.gcc
];
}
```
---
```nix
let
bar = { bacon ? 10, eggs, ... }: bacon + eggs;
in
bar { eggs = 15; bread = 30 }
```
```nix
let
bar = { bacon ? 10, ... }@arguments:
bacon + arguments.eggs;
in
bar { eggs = 15; bread = 30; }
```
---
```nix
outputs = inputs @ {
nixpkgs,
home-manager,
zen-browser,
...
}: {
nixosConfigurations = {
nixos = nixpkgs.lib.nixosSystem {
...
```
---
# Nix writers
* Jednoduchý způsob přidávání skriptů v různých programovacích jazycích do
systému
* Rozdíl oproti klasickému `pkgs.writeShellScriptBin`
* [nixpkgs](https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/writers/scripts.nix) - pro všechny možnosti konfigurace skriptů
* [https://nixos.wiki/wiki/Nix-writers](https://nixos.wiki/wiki/Nix-writers) - wiki
---
```nix
pkgs.writers.writeBash "hello-world-bash" {}
''
echo "Hello world!"
''
pkgs.writers.writePython3 "hello-world-python" {}
''
print("Hello world!")
''
pkgs.writers.writeRust "hello-world-rust" {}
''
fn main() {
println!("Hello world!")
}
''
```
---
```nix
pkgs.writers.writePython3 "hello-world-python"
{
libraries = [ pkgs.python3Packages.requests ];
makeWrapperArgs = [
"--prefix", "PATH", ":", "${pkgs.sl}/bin",
];
}
''
print("Hello world!")
''
```