Building Packages with Nix
Anatomy of a Nix Package
{ stdenv, fetchurl, perl }:
stdenv.mkDerivation {
name = "hello-2.12";
# Source code
src = fetchurl {
url = "https://ftp.gnu.org/gnu/hello/hello-2.12.tar.gz"\;
hash = "sha256-zoJ2IvKo2ioO2U13kTX780rP2MaGwIGcbFOvG+Oi17Y=";
};
# Build-time dependencies
buildInputs = [ perl ];
# Configuration flags
configureFlags = [ "--with-debug" ];
# Custom build steps (if default is insufficient)
buildPhase = ''
make -j $NIX_BUILD_CORES
'';
# Installation instructions
installPhase = ''
make install
mkdir -p $out/share/doc
cp README* $out/share/doc/
'';
# Meta information for package discovery and maintenance
meta = {
description = "A program that produces a friendly greeting";
homepage = "https://www.gnu.org/software/hello/"\;
license = stdenv.lib.licenses.gpl3Plus;
maintainers = [ "example@example.com" ];
platforms = stdenv.lib.platforms.all;
};
}Common Build Phases in Nix
Phase
Default Action
Custom Example
Fetching Source Code
Real-World Package Examples
Simple Python Application
Node.js Web Application
Go CLI Tool
DevOps Patterns for Package Management
Creating a Private Package Repository
Patching Existing Packages
Creating Runtime Wrappers
Versioning and Dependency Management
Pinning Dependencies
Managing Multiple Versions
Testing and CI Integration
Testing Packages in Isolation
CI Configuration for Package Building
Advanced Topics
Cross-Compilation
Static Linking
Creating Minimal Docker Images
Best Practices for Package Maintainability
Conclusion
Further Resources
Last updated