AgentSkillsCN

nixpkgs

适用于使用 Composition API、Nuxt 3、Naive UI 或 Quasar 构建 Vue 3 应用程序时使用。可用于 Pinia、TypeScript、PWA、Capacitor 移动应用,以及 Vite 配置。

SKILL.md
--- frontmatter
name: nixpkgs
description: Use when creating packages (mkDerivation), using the Nixpkgs library (lib), or writing overlays.

Nixpkgs & Standard Library Reference

Overview

Reference for the Nixpkgs standard library (lib) functions and package creation using mkDerivation.

1. Nixpkgs Library (lib)

Access via pkgs.lib or just lib if passed in arguments.

Common Functions

CategoryFunctionUsage / Example
ListsforEachforEach [1 2] (x: x*2) -> [2 4]
filterfilter (x: x>1) [1 2] -> [2]
uniqueunique [1 1 2] -> [1 2]
optionalsoptionals bool [ "a" ] -> ["a"] or []
foldl'foldl' (acc: x: acc + x) 0 [1 2 3] -> 6
StringsconcatStringsSepconcatStringsSep "," ["a" "b"] -> "a,b"
hasPrefixhasPrefix "foo" "foobar" -> true
removeSuffixremoveSuffix ".nix" "file.nix" -> "file"
lowerCharsstringToCharacters "abc" (Use toLower for case)
AttrsmapAttrsmapAttrs (k: v: v*2) { a=1; } -> { a=2; }
recursiveUpdaterecursiveUpdate {a.b=1;} {a.c=2;} -> {a={b=1;c=2;}}
optionalAttrsoptionalAttrs bool { a=1; } -> { a=1; } or {}
attrNamesattrNames { a=1; } -> ["a"]
Debugtracetrace "Output this" value -> Prints + returns value
traceValtraceVal x -> Prints x and returns x

2. Package Creation (mkDerivation)

Template for packaging software with stdenv.

nix
{ lib, stdenv, fetchFromGitHub, openssl, pkg-config, cmake }:

stdenv.mkDerivation rec {
  pname = "my-package";
  version = "1.0.0";

  src = fetchFromGitHub {
    owner = "user";
    repo = "repo";
    rev = "v${version}";
    hash = "sha256-PLACEHOLDER"; # Use lib.fakeSha256 first to get hash
  };

  # Build-time tools (compilers, linters, setup hooks)
  # run on build machine
  nativeBuildInputs = [ 
    pkg-config 
    cmake 
  ];

  # Runtime dependencies (libraries, linked binaries)
  # run on host machine
  buildInputs = [ 
    openssl 
  ];

  # Common phases (override only if needed)
  # configurePhase = ''./configure --prefix=$out'';
  # buildPhase = ''make'';
  
  # Custom install phase example
  installPhase = ''
    runHook preInstall
    
    mkdir -p $out/bin
    cp bin/my-tool $out/bin/
    
    runHook postInstall
  '';

  meta = with lib; {
    description = "A short description";
    homepage = "https://github.com/user/repo";
    license = licenses.mit;
    maintainers = with maintainers; [ maintainer-name ];
    platforms = platforms.linux;
  };
}

Dependency Types Reference

AttributeUse ForExamples
nativeBuildInputsTools needed during build (host platform)pkg-config, cmake, ninja, git
buildInputsLibraries/tools linked at runtime (target platform)openssl, gtk3, glibc, python3
propagatedBuildInputsDependencies needed by dependentsPython libs, Rust libs

3. Overlays

How to modify or extend nixpkgs.

nix
final: prev: {
  # Add new package
  my-package = final.callPackage ./pkgs/my-package {};

  # Override existing package
  hello = prev.hello.overrideAttrs (old: {
    src = ...;
    patches = (old.patches or []) ++ [ ./fix.patch ];
  });
}