Files
wireguard-go/dev-setup.md
T
Your Name c353e76058 add
2025-07-27 22:55:43 +08:00

5.2 KiB

WireGuard Go Development Environment

This repository includes a comprehensive Nix flake development environment with all the tools needed for efficient Go development.

🚀 Quick Start

Prerequisites

  • Nix with flakes enabled
  • direnv (optional but recommended)

Setup

  1. Clone and enter the repository:

    git clone <repo-url>
    cd wireguard-go
    
  2. Option A: Using direnv (Recommended)

    direnv allow
    

    This will automatically load the development environment when you enter the directory.

  3. Option B: Manual activation

    nix develop
    

🔧 Included Tools

Core Go Tools

  • Go 1.23.1 - Matching the project's go.mod
  • gopls - Official Go Language Server for LSP support

Code Quality

  • golangci-lint - Comprehensive linter with 30+ linters enabled
  • staticcheck - Advanced static analysis
  • gosec - Security vulnerability scanner
  • govulncheck - Official Go vulnerability scanner
  • gofumpt - Stricter version of gofmt

Development Tools

  • delve - Go debugger
  • air - Live reload for development
  • gotests - Automatic test generation
  • gomodifytags - Struct tag manipulation
  • impl - Interface implementation generator
  • gotestsum - Enhanced test output

System Tools

  • wireguard-tools - WireGuard utilities
  • iproute2 - Network configuration tools
  • iptables - Firewall utilities

🎯 Quick Commands

Development Workflow

# Install/update dependencies
go mod tidy

# Run comprehensive linting
golangci-lint run

# Check for security vulnerabilities
govulncheck ./...

# Run tests with coverage
go test -race -coverprofile=coverage.out ./...

# Generate tests for a package
gotests -all -w ./device

# Start live reload development server
air

# Format code with stricter rules
gofumpt -w .

Building and Testing

# Build the project
go build .

# Run all tests
go test ./...

# Run tests with race detection
go test -race ./...

# Benchmark tests
go test -bench=. ./...

# Generate coverage report
go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out

Debugging

# Start delve debugger
dlv debug

# Debug a specific test
dlv test ./device

📝 Editor Integration

VS Code

A .vscode/settings.json is included with optimized settings for Go development:

  • Automatic formatting with gofumpt
  • Integrated linting with golangci-lint
  • Proper LSP configuration
  • Optimized file watching and exclusions

Other Editors

For vim/neovim, emacs, or other editors that support LSP:

  • Use gopls as the language server
  • Point formatters to use gofumpt instead of gofmt
  • Configure linting to use golangci-lint

🔍 Code Quality Configuration

Linting

The included .golangci.yml enables 30+ linters with sensible defaults:

  • Security checks (gosec, G-prefixed rules)
  • Performance optimizations (prealloc, ineffassign)
  • Style consistency (gofumpt, goimports)
  • Bug prevention (errcheck, staticcheck)

Pre-commit Hooks (Optional)

Consider setting up pre-commit hooks:

# Create .git/hooks/pre-commit
#!/bin/bash
set -e
golangci-lint run
go test ./...
govulncheck ./...

🌍 Environment Variables

The flake automatically sets up:

  • GOPATH="$PWD/.go"
  • GOBIN="$PWD/.go/bin"
  • GOCACHE="$PWD/.gocache"
  • GO111MODULE=on
  • CGO_ENABLED=1
  • WG_COLOR_MODE=always

🧪 Testing

Running Tests

# All tests
go test ./...

# With race detection
go test -race ./...

# Verbose output
go test -v ./...

# Specific package
go test ./device

# With coverage
go test -coverprofile=coverage.out ./...

Test Generation

# Generate tests for all functions in a package
gotests -all -w ./device

# Generate tests for specific functions
gotests -only FunctionName -w ./device

🔒 Security

Vulnerability Scanning

# Scan for known vulnerabilities
govulncheck ./...

# Security-focused linting
gosec ./...

WireGuard-Specific Security

The environment includes networking tools for testing:

  • WireGuard tools for protocol testing
  • Network namespace utilities
  • Traffic analysis tools

📦 Building Packages

Development Build

go build .

Optimized Build

go build -ldflags="-w -s" .

Using Nix to Build

# Build using the included Nix package
nix build

# The binary will be in ./result/bin/

🐛 Troubleshooting

Common Issues

  1. "command not found" errors

    • Ensure you're in the flake environment: nix develop
    • Or allow direnv: direnv allow
  2. Go module issues

    • Clean module cache: go clean -modcache
    • Verify modules: go mod verify
  3. LSP not working

    • Restart your editor
    • Check gopls is available: which gopls
    • Verify Go version: go version

Performance Tips

  • Use .gocache for faster builds (already configured)
  • Exclude build artifacts from file watchers
  • Use gotestsum for faster test feedback

📚 Additional Resources