Dari Dasar hingga Mahir
4 Minggu - 20 Hari - Complete Guide
Nix adalah package manager powerful yang juga bisa digunakan sebagai OS configuration tool
| Hari | Topik | Praktik |
|---|---|---|
| 1 | Pengenalan Nix | Apa itu Nix? Sejarah, ekosistem |
| 2 | Instalasi | Install Nix di Linux/macOS/WSL |
| 3 | Package Management Dasar | nix-env -i, nix-search, nix-channel |
| 4 | Run Commands | nix run, nix-shell |
| 5 | Nix Store | Memahami /nix/store |
nix - Package managernix-darwin - macOS configurationNixOS - Linux distribution# Install Nix
sh <(curl -L https://nixos.org/nix/install) --no-daemon
# Aktifkan flakes (edit ~/.config/nix/nix.conf)
experimental-features = nix-command flakes
# Verify
nix --version
# Buat config directory
mkdir -p ~/.config/nix
# Edit nix.conf
echo "experimental-features = nix-command flakes" > ~/.config/nix/nix.conf
# Cari package
nix search nixpkgs vim
# Install package (ke user profile)
nix-env -iA nixpkgs.vim
# List installed packages
nix-env -q
# Uninstall
nix-env -e vim
# Upgrade semua
nix-env -u
# Run tanpa install
nix run nixpkgs#vim
# Shell sementara dengan dependencies
nix-shell -p vim git
nix run untuk mencoba aplikasi tanpa menginstall
/nix/store/
βββ 1a2b3c...-vim-9.0/ # Each package has hash
βββ 4d5e6f...-gcc-13.2.0/
βββ ...hash...-glibc-2.38/
# Garbage collection
nix-collect-garbage -d
# List generations
nix-env --list-generations
| Hari | Topik | Praktik |
|---|---|---|
| 1 | Nix Language Basics | Syntax, types, functions |
| 2 | Nix Expressions | Memahami .nix files |
| 3 | Build dari awal | Derivations>|
| 4 | Build Pipeline | Builder scripts, phases |
| 5 | Kuis - Latihan | Build package sederhana |
123 # integer
"hello" # string
[1 2 3] # list
{ a = 1; b = 2; } # attribute set
true / false # boolean
let
x = 1;
y = 2;
in x + y # = 3
add = x: y: x + y
add 1 2 # = 3
# With statement
with builtins; min 1 2 # = 1
# If
if true then "yes" else "no"
# String interpolation
name = "world";
"hello ${name}" # = "hello world"
{ pkgs ? import {} }:
pkgs.mkShell {
name = "hello-env";
buildInputs = [ pkgs.hello pkgs.git ];
}
# Build dan masuk shell
nix-shell hello.nix
# Atau build
nix-build hello.nix
./result/bin/hello
{ stdenv, fetchurl }:
stdenv.mkDerivation rec {
pname = "hello";
version = "2.12.1";
src = fetchurl {
url = "https://ftp.gnu.org/hello/hello-${version}.tar.gz";
sha256 = "sha256-...=";
};
configureFlags = [ "--prefix=${placeholder "out"}" ];
meta = with stdenv.lib; {
description = "A GNU Hello world program";
homepage = "https://www.gnu.org/software/hello/";
license = licenses.gpl3;
};
}
nix-build my-derivation.nix
| Hari | Topik | Praktik |
|---|---|---|
| 1 | NixOS Concepts | Declarative system configuration |
| 2 | Configuration.nix | Setup dasar (users, packages, services) |
| 3 | Modules | Membuat module sendiri |
| 4 | Option Types | Types di NixOS options |
| 5 | Modules Reusable | Import/export modules |
{ config, pkgs, ... }:
{
# Boot
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/sda";
# Networking
networking.hostName = "myhost";
networking.firewall.allowedTCPPorts = [ 80 443 ];
# Users
users.users.alice = {
isNormalUser = true;
extraGroups = [ "wheel" ];
};
# Packages
environment.systemPackages = with pkgs; [
vim
git
curl
];
# Services
services.openssh.enable = true;
services.nginx.enable = true;
# Timezone
time.timeZone = "Asia/Jakarta";
}
# Apply configuration
sudo nixos-rebuild switch
# Rollback
sudo nixos-rebuild switch --rollback
# Upgrade
sudo nixos-rebuild switch --upgrade
| Hari | Topik | Praktik |
|---|---|---|
| 1 | Flakes Intro | Apa itu flakes? flake.nix, flake.lock |
| 2 | NixOS Modules with Flakes | Modern NixOS setup |
| 3 | Home Manager | User config dengan Nix |
| 4 | DevShells | Development environments |
| 5 | Project Summary | Deploy project nyata |
{
description = "My NixOS Configuration";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, home-manager, ... }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
system = system;
modules = [ ./configuration.nix ];
};
};
}
nix flake show # Show flake outputs
nix flake update # Update inputs
devShells.${system}.default = pkgs.mkShell {
packages = [
pkgs.nodejs_20
pkgs.yarn
pkgs.python3
pkgs.postgresql
];
# Environment variables
GREETING = "Hello from Nix!";
# Shell hook
shellHook = ''
echo "Dev shell activated!"
'';
};
# Enter dev shell
nix develop
# Or with custom name
nix develop .#myapp
{ config, pkgs, ... }:
{
home.username = "alice";
home.homeDirectory = "/home/alice";
# Packages
home.packages = with pkgs; [
vim
fish
starship
];
# Programs
programs.fish.enable = true;
programs.starship.enable = true;
programs.git.enable = true;
# Dotfiles
home.file.".vimrc".source = ./vimrc;
home.stateVersion = "24.05";
}
nix search nixpkgs <package> # Cari package
nix-env -iA nixpkgs.<package> # Install
nix-env -e <package> # Uninstall
nix-env -q # List installed
nix-shell -p <packages> # Shell dengan packages
nix run nixpkgs#<package> # Run tanpa install
nix-build <file.nix> # Build derivation
nix develop # Enter dev shell
nixos-rebuild switch # Apply config
nixos-rebuild switch --rollback # Rollback
nix-collect-garbage -d # Clean up
{ config, pkgs, lib, ... }:
{
imports = [
# Import other modules
./hardware-configuration.nix
./networking.nix
];
# Boot Configuration
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/sda";
boot.loader.grub.efiSupport = true;
boot.loader.grub.efiInstallAsRemovable = true;
# Systemd Services
systemd.services.my-service = {
enable = true;
description = "My Custom Service";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${pkgs.myapp}/bin/myapp";
Restart = "always";
};
};
# Users & Groups
users.users.alice = {
isNormalUser = true;
description = "Alice User";
extraGroups = [ "wheel" "docker" "networkmanager" ];
openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAA..." ];
};
# Environment Variables
environment.sessionVariables = {
EDITOR = "vim";
VISUAL = "vim";
};
# Timezone & Locale
time.timeZone = "Asia/Jakarta";
i18n.defaultLocale = "en_US.UTF-8";
}
# Enable service
services.myapp.enable = true
# Atau manual
sudo systemctl enable myapp
sudo systemctl start myapp
nixos-option untuk cari opsi yang tersedia!{
description = "My NixOS Config with Flakes";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
# Overlay
my-overlay.url = "github:user/my-overlay";
};
outputs = { self, nixpkgs, home-manager, ... }@inputs:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
# NixOS Configuration
nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
system = system;
specialArgs = { inherit inputs; };
modules = [
./hosts/myhost/configuration.nix
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.users.alice = import ./home/alice.nix;
}
];
};
# DevShell for development
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [
nodejs_20
python3
postgresql
];
};
};
}
# Lock file otomatisηζ
# Commit ini untuk reproducibility!
{
"nodes": {
"nixpkgs": {
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "abc123...",
"type": "github"
}
}
}
}
nix flake init # Buat flake baru
nix flake update # Update inputs
nix flake show # Show outputs
nix flake check # Check flake
nixos-rebuild switch --flake .#myhost
Apa itu nixpkgs?
Apa keunggulan Nix dibanding package manager lain?
Apa fungsi nix-env?