--- marp: true class: invert paginate: true footer: "Albert Vala, Filip Kohout, Nix flakes & Nix command" --- # Nix flakes & Nix command --- # Nix Command * Nové nix cli * Flake support --- # Porovnani ```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"]; # Enable ‘sudo’ for the user. }; system.stateVersion = "25.05"; # Did you read the comment? } ``` --- # 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 --- # 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!") '' ```