コンテンツにスキップ

Nix

Comprehensive nix commands and usage patterns for efficient workflow management.

Overview

Nix is a powerful tool for various operations and system management. This cheat sheet covers essential commands, configuration options, and best practices.

Installation

Linux/Ubuntu

# Package manager installation
sudo apt update
sudo apt install nix

# Alternative installation
wget -O nix https://github.com/example/nix/releases/latest/download/nix-linux
chmod +x nix
sudo mv nix /usr/local/bin/

macOS

# Homebrew installation
brew install nix

# Manual installation
curl -L -o nix https://github.com/example/nix/releases/latest/download/nix-macos
chmod +x nix
sudo mv nix /usr/local/bin/

Windows

# Chocolatey installation
choco install nix

# Scoop installation
scoop install nix

# Manual installation
# Download from official website and add to PATH

Basic Commands

Command Description
nix --help Display help information
nix --version Show version information
nix init Initialize nix in current directory
nix status Check current status
nix list List available options
nix info Display system information
nix config Show configuration
nix update Update to latest version

Essential Operations

Getting Started

# Initialize nix
nix init

# Basic usage
nix run

# With verbose output
nix --verbose run

# With configuration file
nix --config config.yaml run

Configuration

# View configuration
nix config show

# Set configuration option
nix config set key value

# Get configuration value
nix config get key

# Reset configuration
nix config reset

Advanced Operations

# Debug mode
nix --debug run

# Dry run (preview changes)
nix --dry-run run

# Force operation
nix --force run

# Parallel execution
nix --parallel run

File Operations

Command Description
nix create <file> Create new file
nix read <file> Read file contents
nix update <file> Update existing file
nix delete <file> Delete file
nix copy <src> <dst> Copy file
nix move <src> <dst> Move file

Network Operations

# Connect to remote host
nix connect host:port

# Listen on port
nix listen --port 8080

# Send data
nix send --data "message" --target host

# Receive data
nix receive --port 8080

Security Features

Authentication

# Login with credentials
nix login --user username

# Logout
nix logout

# Change password
nix passwd

# Generate API key
nix generate-key

Encryption

# Encrypt file
nix encrypt file.txt

# Decrypt file
nix decrypt file.txt.enc

# Generate certificate
nix cert generate

# Verify signature
nix verify file.sig

Troubleshooting

Common Issues

Issue: Command not found

# Check if installed
which nix

# Reinstall if necessary
sudo apt reinstall nix

Issue: Permission denied

# Run with sudo
sudo nix command

# Fix permissions
chmod +x /usr/local/bin/nix

Issue: Configuration errors

# Reset configuration
nix config reset

# Validate configuration
nix config validate

Debug Commands

Command Description
nix --debug Enable debug output
nix --verbose Verbose logging
nix test Run self-tests
nix doctor Check system health

Best Practices

Security

  • Always verify checksums when downloading
  • Use strong authentication methods
  • Regularly update to latest version
  • Follow principle of least privilege

Performance

  • Use appropriate buffer sizes
  • Monitor resource usage
  • Optimize configuration for your use case
  • Regular maintenance and cleanup

Maintenance

# Update nix
nix update

# Clean temporary files
nix clean

# Backup configuration
nix backup --config

# Restore from backup
nix restore --config backup.yaml

Integration

Scripting

#!/bin/bash
# Example script using nix

if ! command -v nix &> /dev/null; then
    echo "nix is not installed"
    exit 1
fi

if nix run; then
    echo "Success"
else
    echo "Failed"
    exit 1
fi

API Integration

import subprocess
import json

def run_nix(command):
    try:
        result = subprocess.run(['nix'] + command.split(),
                              capture_output=True, text=True)
        return result.stdout
    except Exception as e:
        print(f"Error: \\\\{e\\\\}")
        return None

Environment Variables

Variable Description Default
NIX_CONFIG Configuration file path ~/.nix/config
NIX_HOME Home directory ~/.nix
NIX_LOG_LEVEL Logging level INFO
NIX_TIMEOUT Operation timeout 30s

Configuration File

# ~/.nix/config.yaml
version: "1.0"
settings:
  debug: false
  timeout: 30
  log_level: "INFO"

network:
  host: "localhost"
  port: 8080
  ssl: true

security:
  auth_required: true
  encryption: "AES256"

Examples

Basic Workflow

# 1. Initialize
nix init

# 2. Configure
nix config set host example.com

# 3. Run operation
nix run

# 4. Check results
nix status

# 5. Cleanup
nix clean

Advanced Workflow

# Comprehensive operation
nix run \
  --config production.yaml \
  --parallel \
  --verbose \
  --timeout 300

# Monitoring
nix monitor \
  --interval 60 \
  --alert-threshold 80

Resources

Official Documentation

Community

Tutorials


Last updated: 2025-07-05