面向并行开发的 Nix 构建一次构建处处复用 Nix Builds for Parallel Development Build Once,Reuse Everywhere_第1页
面向并行开发的 Nix 构建一次构建处处复用 Nix Builds for Parallel Development Build Once,Reuse Everywhere_第2页
面向并行开发的 Nix 构建一次构建处处复用 Nix Builds for Parallel Development Build Once,Reuse Everywhere_第3页
面向并行开发的 Nix 构建一次构建处处复用 Nix Builds for Parallel Development Build Once,Reuse Everywhere_第4页
面向并行开发的 Nix 构建一次构建处处复用 Nix Builds for Parallel Development Build Once,Reuse Everywhere_第5页
已阅读5页,还剩35页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Oct.29

IndustrialOpenSource

Oct.30

OpenSourceRobotics

Agenda

NixBuildsfor

ParallelDevelopment

BuildOnce,ReuseEverywhere

29October2026

MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAG

Motivation:WhyNot10xFaster?

AImakescoding10xfaster.Whydoesn’ttheteamship10xfaster?

Theorem(Amdahl’sLaw).Overallspeedupislimitedbythefractionofworkthatremainsunchanged:

p=fractionaccelerated,s=speeduponthatfraction

s=10(illustrative)

•p=30%(coding):1.37x

BuildandCItimeistheconstraint

•p=50%(coding):1.8x

Everythingafterthecodeisunchanged

•Reviewqueuesgrow

•CIandbuildstakethesamewalltime

Speedingupthecodegenerationisonepart.Next:speedupthebuild.

Page2MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Page3MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

TraditionalSolutions

CI-providercaches

Provider-specificAPIsandcachekeys;switchingCIproviderscanmeanreworkingthesetup.Mightnotworklocallyacrossallsystems.

Build-toolcaches

Reusetaskoutputs,locallyorremotely;eachtoolislimitedtoitsownecosystem.

Compilercaches

Hermeticbuildsystems

ccache,sccache:cachecompileroutputs;sccachesupportsremotestorage.Neithercachestoolchainsorsystemlibraries.

Bazel,Buck2:cacheeverything,butadoptionmeansrewritingthebuild;third-partydependenciesare

painful.

SiloedorCostly

Wanted:onecacheandbuild-offloadmodelforalldependencies;wrapexistingbuildsinsteadofrewritingthem.

Page4MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

CanWeDoBetter?

IntroducingNix:Theuniversal,composablebuildsystem

Input-AddressedBuildsruninasandboxBuilds

withoutnetworkaccess;onlydeclaredinputsarevisible.

RichEcosystem

Hermetic

~150,000

packages

;existingbuildsarewrapped,not

rewritten.

Outputpathsderivefromthederivationanditsinputs;thestorepathisthecachekey

(trivialinvalidation).

Reuseinsteadofrebuild

Nixeitherfetchesmatchingstorepathsfromabinarycache

Page5MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

NixLanguage,Tool,andEcosystem

Lazy,purelyfunctionallanguage.

Evaluatesexpressionsintoderivations(buildrecipes:inputs,builder,env→output

paths);managesthestoreandrunsbuilds.

Collectionofpackageexpressionsandbuildfunctions.

Nixlanguage

evaluatedby

nix+nix-daemon

evaluates

Nixpkgs

LinuxdistributionconfiguredwithNixmod-ules.

providesmodulesfor

NixOS

Page6MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

NixLanguage:AQuickPrimer

TheNixlanguageconsistsofexpressionsthatevaluatetovalues:

let

add=a:b:a+b;

libs=["openssl""zlib"];

src=./.;#caveat:

copies.to

storein{

inheritsrc;#src=src;

sum=add12;

names=map(l:"lib-"+l)libs;}

#=>{src=/path/here;sum=3;

#names=["lib-openssl"

#"lib-zlib"];}

•letbindsnames;expressionafterinistheresult

•a:b:exprdefinesafunctionoftwoarguments;add12appliesit

•[...]isaspace-separatedlist;mapappliesafunctiontoeachitem

•./.isapathliteral,notastring;copiedtothestorewhencoercedtoastring(e.g.,inaderivation)

•A{}isanattributeset,themaindatastructureofNix

Page7MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

NixLanguage:AttributeSetsandMerging

let

common={pname="app";version="1";};

mkPkg={pname,version,...}:{inheritpnameversion;

description="${pname}${version}";};

inmkPkg(common//{version="2";})

#=>{pname="app";version="2";

#description="app2";}

{hello="ok";huge=throw"no";}.hello#=>"ok"

•x={a=1;}isaset;x.aselects

•//mergessets(shallow);therightsidewins

•{a,b,}:takesaset

anddestructuresit;...ignoresextraattributes

•inherita;meansa=a;

•"${a}"isstringinterpolation

•Evaluationislazy:hugeisneverevaluated,sonoerror

Page8MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

NixPackages:It'sAllAboutFunctions

ANixpackageisafunction,withtheargumentsbeingitsdependencies.

{craneLib,pkg-config,openssl}:#depsareNixexpressions

let

commonArgs={

src=craneLib.cleanCargoSource./.;#FiltersourcetoCargo-relevantfiles

nativeBuildInputs=[pkg-config];#Build-platformtools

buildInputs=[openssl];#Host-platformdeps(e.g.,libraries)

};

cargoArtifacts=craneLib.buildDepsOnlycommonArgs;#depsonly(release)cargoDebugArtifacts=craneLib.buildDepsOnly(commonArgs//{

env.CARGO_PROFILE="dev";#debugdepstopre-populatedevShelltarget/});

in

craneLib.buildPackage(commonArgs//{

inheritcargoArtifacts;#onlyworkspacecodebuilthere

passthru={inheritcargoDebugArtifacts;};#exposeforCIcache+devShell})

Page9MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Evaluation:FromNixExpressiontoArtifact

Derivationgraph

.drvfiles

Checklocalstore

andbinarycaches

Cachemiss

Buildlocallyoron

remotebuilder

Nixexpressione.g.,package

Cachehit

Reuselocallyorsub-

stitute(=down-

load)fromcache

Immutablestoreoutput

Evaluate

Page10MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Flakes:It'sAllAboutComposition

Nixflakespininputsanddefinepackagesanddevshells:

{inputs.nixpkgs.url="github:NixOS/nixpkgs/nixos-unstable";inputs.crane.url="github:ipetkov/crane";

outputs={nixpkgs,crane,...}:letsystem="x86_64-linux";

pkgs=importnixpkgs{inheritsystem;};

craneLib=crane.mkLibpkgs;

my-app=pkgs.callPackage./package.nix{inheritcraneLib;};in{

packages.${system}={

inheritmy-app;

deps-debug=my-app.cargoDebugArtifacts;#forCItopopulatecache};

devShells.${system}.default=craneLib.devShell{inputsFrom=[my-app];};};

}

Page11MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Innerloop

•Enterdevshell:nixdevelop.

•Runtoolsdirectly:cargocheck,cargotest

•Pre-populatewithCI-builtdependencies

DX:TwoFeedbackLoops

Outerloop

•Verifybuild:nixbuild.#my-app

•Isolatedderivationcatchesundeclaredinputs

Page12MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

InnerLoop:Pre-populateRusttarget/fromaBinaryCache

•

•

•

•

•

Buildalldependenciesonce(e.g.,inCI)andreusethemforlocalbuilds:

let

deps=my-app.cargoDebugArtifacts;

prepopulate=''

STAMP="target/.deps-stamp"

if["$(cat"$STAMP"2>/dev/null)"\!="${deps}"];then

mkdir-ptarget

tar-Izstd-Ctarget-xf\${deps}/target.tar.zst

chmod-Ru+wtarget

echo-n"${deps}">"$STAMP"fi

#+pointCARGO_HOMEatvendoredsources'';

in

devShells.${system}.default=craneLib.devShell{inputsFrom=[my-app];

packages=[pkgs.zstd];

shellHook=prepopulate;};

nixdevelopsubstitutes

cargoDebugArtifacts(publishedvia.#deps-debug)andunpacksitintotarget/

cargobuildcompilesworkspacecrates

Deletetarget/freely,depsstaycachedin/nix/store

Needsidenticalrustc/flagsandvendoredstorepaths,elsefullrebuild

Forper-cratecachereuse,usecrate2nix

Page13MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

CachingStrategy

substituteBinaryCachessign+push

DevelopersCISystems

substitute

Remote

offloadbuildsBuilder

Signedstorepaths

BinaryCaches

•Upstream:

(read-only,S3-backed)

•Mirrors:e.g.,USTC,TUNA,SJTU(read-only)

•Self-hosted:Attic,Harmonia

•SaaS:Cachix,FlakeHubCache

KeyMechanics

•Trust:artifactsarecryptographicallysigned

•Builders:accessviaSSH

•CIsavings:cachehitscutbuildtimeandCIminutes

•Caching:optimization,notharddependency

Page14MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

MeasuredResults

Scenario

PlainCargo

Warmsccache

Nix,pre-populated

Freshtarget,walltime

7.59s

5.06s

2.36s

Freshtarget,CPUtime

42.13s

20.28s

4.69s

4parallelbuilds,lastfinish

18.57s

9.67s

4.71s

4parallelbuilds,totalCPU

249.85s

116.76s

28.87s

4parallelbuilds,peakRSS

4,045MiB

2,787MiB

1,780MiB

Incrementalleaf,walltime

0.51s

0.52s

0.51s

Incrementalleaf,CPUtime

0.82s

0.67s

0.72s

Takeaway

•Fresh-buildCPU:9×faster

•4parallelbuilds,CPU:8.7×faster

•Incrementalrebuilds:comparable

Amdahl:end-to-endgain

Speedupon4parallelbuilds:s=18.57/4.71≈3.9

•Builds+CIwaitp=20%ofcycle:1.18x

•p=40%:1.42xMeasureyourownp.

Five-runmedians,16CPUs,32GiBRAM.Smallone-cratelocalcheck,noremotecacheorbuilder;notascaleforecast.

Page15MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Conclusion

Onepinnedflakedefinespackagesanddevshells;binarycacheandremotebuildersreuseitsoutputs.

Benefits

•Samedeclareddependenciesindevelopment,agents,andCI

•Matchingoutputssharedacrossmachinesinsteadofrebuilt

•Buildscanbetriviallyoffloadedtoremotebuilders

Costs

•Steeplearningcurve(thoughLLMsgreatlylowerit)

•Coarseinvalidation:anyCargo.lockchangerebuildsallofcargoDebugArtifacts

•nixbuildisnotincremental:alatefailurererunsthederivation

•Pre-populatingbuilddirectoriesis

per-languagework;Nixevaladdsoverhead

Page16MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Thanks

Questions?

MichaelAdler

michael.adler@

Page17MichaelAdler|SeniorKeyExpert|FTRPDCPCOES-DE|SiemensAGSIEMENS

Backup:SandboxedAgen

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论