presentation-nix-flakes/prezentace.md
2025-03-14 09:31:44 +01:00

239 lines
4.6 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
* Nove nix cli
* Flake support
---
# Porovnani
```shell
nix-shell -p cowsay #Stare
nix shell nixpkgs#cowsay #Nove
nix run nixpkgs#cowsay #Nove
```
---
# Commandy
| Command | Na |
| ------------ | ---------------------------------- |
| flake new | Vytvoreni flaky z template |
| develop | Build environment shell |
| shell | temp shell s nejakym programem |
| config check | Check configu na potencialni chyby |
| run | Spusti output z flaky |
---
# Flakes
* ## Proc?
* Lockfiles.
* Flake inputs
* ## Na co?
* Cokoliv.
---
# Jak pouzit flakes na system configuraci?
* Flake je jen cast zbytek je v normalnich nix filech
* Zbytek konfigurace bere inputy z flaky.
* Neprijdeme o nic jen mame lock file a moznost flake jako inputu
---
# Flake example
```nix
{
description = "NixOS configuration";
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 = {
laptop = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./laptop/configuration.nix
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = false;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {inherit zen-browser;};
home-manager.users.albert = import ./modules/home.nix;
}
];
};
};
};
}
```
---
```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;
}
```
---
# 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 flake show` - zobrazí flake strukturu
```nix
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=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
---
# Virtual machine
`nix run github:nix-community/nixos-generators -- -c ./flake.nix --flake '#default' -f vm --disk-size 20480`
---
# Nix writers
- Jednoduchý způsob přidávání skriptů v různých programovacích jazycích do
systému ve flake konfiguraci
- 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) -
osekaná 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!")
''
```