Home
Random
Log in
Settings
About the NixOS Wiki
NixOS Wiki
Search
Export translations
Translate
Settings
Group
Accelerated Video Playback
Appimage
Applications
Category:Community
Category:Desktop environment
Category:Hardware
Category:Server
Category:Software
Creating a NixOS live CD
Disko
Doas
Docker
FAQ
Firefox
Flakes
Hardware/System76/galp5
Honk
Hugo
Hydra
Install NixOS on Amazon EC2
Kitty
Konqueror
MediaWiki:Sidebar
Mihomo
Neovim
Nitrokey
Nix (language)
Nix (package manager)
Nix ecosystem
NixOS
NixOS as a desktop
NixOS Installation Guide
NixOS on ARM
NixOS on ARM/UEFI
NixOS Wiki
NixOS Wiki/documentation topics
NixOS Wiki/get in touch
NixOS Wiki/getting started
NixOS Wiki/setting up
NixOS Wiki/special pages
NixOS Wiki/welcome
NixOS Wiki:News
Nixpkgs
Packaging/Icons
Plymouth
Secure Boot
Smartmontools
Starship
Steam
Sudo
Systemd/Hardening
Systemd/logind
Systemd/timers
Systemd/User Services
Terms and Definitions in Nix Project
Vim
VR
Language
aa - Qafár af
aae - Arbërisht
ab - аԥсшәа
abs - bahasa ambon
ace - Acèh
acf - Kwéyòl Sent Lisi
acm - عراقي
ady - адыгабзэ
ady-cyrl - адыгабзэ
aeb - تونسي / Tûnsî
aeb-arab - تونسي
aeb-latn - Tûnsî
af - Afrikaans
aln - Gegë
alt - алтай тил
am - አማርኛ
ami - Pangcah
an - aragonés
ang - Ænglisc
ann - Obolo
anp - अंगिका
apc - شامي
ar - العربية
arc - ܐܪܡܝܐ
arn - mapudungun
arq - جازايرية
ary - الدارجة
arz - مصرى
as - অসমীয়া
ase - American sign language
ast - asturianu
atj - Atikamekw
av - авар
avk - Kotava
awa - अवधी
ay - Aymar aru
az - azərbaycanca
azb - تۆرکجه
ba - башҡортса
ban - Basa Bali
ban-bali - ᬩᬲᬩᬮᬶ
bar - Boarisch
bbc - Batak Toba
bbc-latn - Batak Toba
bcc - جهلسری بلوچی
bci - wawle
bcl - Bikol Central
bdr - Bajau Sama
be - беларуская
be-tarask - беларуская (тарашкевіца)
bew - Betawi
bg - български
bgc - हरियाणवी
bgn - روچ کپتین بلوچی
bh - भोजपुरी
bho - भोजपुरी
bi - Bislama
bjn - Banjar
blk - ပအိုဝ်ႏဘာႏသာႏ
bm - bamanankan
bn - বাংলা
bo - བོད་ཡིག
bpy - বিষ্ণুপ্রিয়া মণিপুরী
bqi - بختیاری
br - brezhoneg
brh - Bráhuí
bs - bosanski
btm - Batak Mandailing
bto - Iriga Bicolano
bug - Basa Ugi
bxr - буряад
ca - català
cbk-zam - Chavacano de Zamboanga
ccp - 𑄌𑄋𑄴𑄟𑄳𑄦
cdo - 閩東語 / Mìng-dĕ̤ng-ngṳ̄
ce - нохчийн
ceb - Cebuano
ch - Chamoru
chn - chinuk wawa
cho - Chahta anumpa
chr - ᏣᎳᎩ
chy - Tsetsêhestâhese
ckb - کوردی
co - corsu
cps - Capiceño
cpx - 莆仙語 / Pó-sing-gṳ̂
cpx-hans - 莆仙语(简体)
cpx-hant - 莆仙語(繁體)
cpx-latn - Pó-sing-gṳ̂ (Báⁿ-uā-ci̍)
cr - Nēhiyawēwin / ᓀᐦᐃᔭᐍᐏᐣ
crh - qırımtatarca
crh-cyrl - къырымтатарджа (Кирилл)
crh-latn - qırımtatarca (Latin)
crh-ro - tatarşa
cs - čeština
csb - kaszëbsczi
cu - словѣньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ
cv - чӑвашла
cy - Cymraeg
da - dansk
dag - dagbanli
de - Deutsch
de-at - Österreichisches Deutsch
de-ch - Schweizer Hochdeutsch
de-formal - Deutsch (Sie-Form)
dga - Dagaare
din - Thuɔŋjäŋ
diq - Zazaki
dsb - dolnoserbski
dtp - Kadazandusun
dty - डोटेली
dua - Duálá
dv - ދިވެހިބަސް
dz - ཇོང་ཁ
ee - eʋegbe
efi - Efịk
egl - Emiliàn
el - Ελληνικά
eml - emiliàn e rumagnòl
en - English
en-ca - Canadian English
en-gb - British English
eo - Esperanto
es - español
es-419 - español de América Latina
es-formal - español (formal)
et - eesti
eu - euskara
ext - estremeñu
fa - فارسی
fat - mfantse
ff - Fulfulde
fi - suomi
fit - meänkieli
fj - Na Vosa Vakaviti
fo - føroyskt
fon - fɔ̀ngbè
fr - français
frc - français cadien
frp - arpetan
frr - Nordfriisk
fur - furlan
fy - Frysk
ga - Gaeilge
gaa - Ga
gag - Gagauz
gan - 贛語
gan-hans - 赣语(简体)
gan-hant - 贛語(繁體)
gcf - kréyòl Gwadloup
gcr - kriyòl gwiyannen
gd - Gàidhlig
gl - galego
gld - на̄ни
glk - گیلکی
gn - Avañe'ẽ
gom - गोंयची कोंकणी / Gõychi Konknni
gom-deva - गोंयची कोंकणी
gom-latn - Gõychi Konknni
gor - Bahasa Hulontalo
got - 𐌲𐌿𐍄𐌹𐍃𐌺
gpe - Ghanaian Pidgin
grc - Ἀρχαία ἑλληνικὴ
gsw - Alemannisch
gu - ગુજરાતી
guc - wayuunaiki
gur - farefare
guw - gungbe
gv - Gaelg
ha - Hausa
hak - 客家語 / Hak-kâ-ngî
hak-hans - 客家语(简体)
hak-hant - 客家語(繁體)
hak-latn - Hak-kâ-ngî (Pha̍k-fa-sṳ)
haw - Hawaiʻi
he - עברית
hi - हिन्दी
hif - Fiji Hindi
hif-latn - Fiji Hindi
hil - Ilonggo
hno - ہندکو
ho - Hiri Motu
hr - hrvatski
hrx - Hunsrik
hsb - hornjoserbsce
hsn - 湘語
ht - Kreyòl ayisyen
hu - magyar
hu-formal - magyar (formal)
hy - հայերեն
hyw - Արեւմտահայերէն
hz - Otsiherero
ia - interlingua
iba - Jaku Iban
ibb - ibibio
id - Bahasa Indonesia
ie - Interlingue
ig - Igbo
igl - Igala
ii - ꆇꉙ
ik - Iñupiatun
ike-cans - ᐃᓄᒃᑎᑐᑦ
ike-latn - inuktitut
ilo - Ilokano
inh - гӀалгӀай
io - Ido
is - íslenska
isv-cyrl - меджусловјанскы
isv-latn - medžuslovjansky
it - italiano
iu - ᐃᓄᒃᑎᑐᑦ / inuktitut
ja - 日本語
jam - Patois
jbo - la .lojban.
jut - jysk
jv - Jawa
ka - ქართული
kaa - Qaraqalpaqsha
kab - Taqbaylit
kai - Karai-karai
kbd - адыгэбзэ
kbd-cyrl - адыгэбзэ
kbp - Kabɩyɛ
kcg - Tyap
kea - kabuverdianu
kg - Kongo
kge - Kumoring
khw - کھوار
ki - Gĩkũyũ
kiu - Kırmancki
kj - Kwanyama
kjh - хакас
kjp - ဖၠုံလိက်
kk - қазақша
kk-arab - قازاقشا (تٴوتە)
kk-cn - قازاقشا (جۇنگو)
kk-cyrl - қазақша (кирил)
kk-kz - қазақша (Қазақстан)
kk-latn - qazaqşa (latın)
kk-tr - qazaqşa (Türkïya)
kl - kalaallisut
km - ភាសាខ្មែរ
kn - ಕನ್ನಡ
knc - Yerwa Kanuri
ko - 한국어
ko-kp - 조선말
koi - перем коми
kr - kanuri
krc - къарачай-малкъар
kri - Krio
krj - Kinaray-a
krl - karjal
ks - कॉशुर / کٲشُر
ks-arab - کٲشُر
ks-deva - कॉशुर
ksh - Ripoarisch
ksw - စှီၤ
ku - kurdî
ku-arab - کوردی (عەرەبی)
ku-latn - kurdî (latînî)
kum - къумукъ
kus - Kʋsaal
kv - коми
kw - kernowek
ky - кыргызча
la - Latina
lad - Ladino
lb - Lëtzebuergesch
lbe - лакку
lez - лезги
lfn - Lingua Franca Nova
lg - Luganda
li - Limburgs
lij - Ligure
liv - Līvõ kēļ
lki - لەکی
lld - Ladin
lmo - lombard
ln - lingála
lo - ລາວ
loz - Silozi
lrc - لۊری شومالی
lt - lietuvių
ltg - latgaļu
lua - ciluba
lus - Mizo ţawng
luz - لئری دوٙمینی
lv - latviešu
lzh - 文言
lzz - Lazuri
mad - Madhurâ
mag - मगही
mai - मैथिली
map-bms - Basa Banyumasan
mdf - мокшень
mg - Malagasy
mh - Ebon
mhr - олык марий
mi - Māori
min - Minangkabau
mk - македонски
ml - മലയാളം
mn - монгол
mnc - manju gisun
mnc-latn - manju gisun
mnc-mong - ᠮᠠᠨᠵᡠ ᡤᡳᠰᡠᠨ
mni - ꯃꯤꯇꯩ ꯂꯣꯟ
mnw - ဘာသာမန်
mo - молдовеняскэ
mos - moore
mr - मराठी
mrh - Mara
mrj - кырык мары
ms - Bahasa Melayu
ms-arab - بهاس ملايو
mt - Malti
mui - Baso Palembang
mus - Mvskoke
mwl - Mirandés
my - မြန်မာဘာသာ
myv - эрзянь
mzn - مازِرونی
na - Dorerin Naoero
nah - Nāhuatl
nan - 閩南語 / Bân-lâm-gú
nan-hant - 閩南語(傳統漢字)
nan-latn-pehoeji - Bân-lâm-gú (Pe̍h-ōe-jī)
nan-latn-tailo - Bân-lâm-gú (Tâi-lô)
nap - Napulitano
nb - norsk bokmål
nds - Plattdüütsch
nds-nl - Nedersaksies
ne - नेपाली
new - नेपाल भाषा
ng - Oshiwambo
nia - Li Niha
nit - కొలామి
niu - Niuē
nl - Nederlands
nl-informal - Nederlands (informeel)
nmz - nawdm
nn - norsk nynorsk
no - norsk
nod - ᨣᩤᩴᨾᩮᩬᩥᨦ
nog - ногайша
nov - Novial
nqo - ߒߞߏ
nr - isiNdebele seSewula
nrm - Nouormand
nso - Sesotho sa Leboa
nup - Nupe
nv - Diné bizaad
ny - Chi-Chewa
nyn - runyankore
nyo - Orunyoro
nys - Nyunga
oc - occitan
ojb - Ojibwemowin
olo - livvinkarjala
om - Oromoo
or - ଓଡ଼ିଆ
os - ирон
pa - ਪੰਜਾਬੀ
pag - Pangasinan
pam - Kapampangan
pap - Papiamentu
pcd - Picard
pcm - Naijá
pdc - Deitsch
pdt - Plautdietsch
pfl - Pälzisch
pi - पालि
pih - Norfuk / Pitkern
pl - polski
pms - Piemontèis
pnb - پنجابی
pnt - Ποντιακά
prg - prūsiskan
ps - پښتو
pt - português
pt-br - português do Brasil
pwn - pinayuanan
qu - Runa Simi
qug - Runa shimi
rgn - Rumagnôl
rif - Tarifit
rki - ရခိုင်
rm - rumantsch
rmc - romaňi čhib
rmy - romani čhib
rn - ikirundi
ro - română
roa-tara - tarandíne
rsk - руски
ru - русский
rue - русиньскый
rup - armãneashti
ruq - Vlăheşte
ruq-cyrl - Влахесте
ruq-latn - Vlăheşte
rut - мыхаӀбишды
rw - Ikinyarwanda
ryu - うちなーぐち
sa - संस्कृतम्
sah - саха тыла
sat - ᱥᱟᱱᱛᱟᱲᱤ
sc - sardu
scn - sicilianu
sco - Scots
sd - سنڌي
sdc - Sassaresu
sdh - کوردی خوارگ
se - davvisámegiella
se-fi - davvisámegiella (Suoma bealde)
se-no - davvisámegiella (Norgga bealde)
se-se - davvisámegiella (Ruoŧa bealde)
sei - Cmique Itom
ses - Koyraboro Senni
sg - Sängö
sgs - žemaitėška
sh - srpskohrvatski / српскохрватски
sh-cyrl - српскохрватски (ћирилица)
sh-latn - srpskohrvatski (latinica)
shi - Taclḥit
shi-latn - Taclḥit
shi-tfng - ⵜⴰⵛⵍⵃⵉⵜ
shn - ၽႃႇသႃႇတႆး
shy - tacawit
shy-latn - tacawit
si - සිංහල
simple - Simple English
sjd - кӣллт са̄мь кӣлл
sje - bidumsámegiella
sk - slovenčina
skr - سرائیکی
skr-arab - سرائیکی
sl - slovenščina
sli - Schläsch
sm - Gagana Samoa
sma - åarjelsaemien
smn - anarâškielâ
sms - nuõrttsääʹmǩiõll
sn - chiShona
so - Soomaaliga
sq - shqip
sr - српски / srpski
sr-ec - српски (ћирилица)
sr-el - srpski (latinica)
srn - Sranantongo
sro - sardu campidanesu
ss - SiSwati
st - Sesotho
stq - Seeltersk
sty - себертатар
su - Sunda
sv - svenska
sw - Kiswahili
syl - ꠍꠤꠟꠐꠤ
szl - ślůnski
szy - Sakizaya
ta - தமிழ்
tay - Tayal
tcy - ತುಳು
tdd - ᥖᥭᥰ ᥖᥬᥲ ᥑᥨᥒᥰ
te - తెలుగు
tet - tetun
tg - тоҷикӣ
tg-cyrl - тоҷикӣ
tg-latn - tojikī
th - ไทย
ti - ትግርኛ
tig - ትግሬ
tk - Türkmençe
tl - Tagalog
tly - tolışi
tly-cyrl - толыши
tn - Setswana
to - lea faka-Tonga
tok - toki pona
tpi - Tok Pisin
tr - Türkçe
tru - Ṫuroyo
trv - Seediq
ts - Xitsonga
tt - татарча / tatarça
tt-cyrl - татарча
tt-latn - tatarça
ttj - Orutooro
tum - chiTumbuka
tw - Twi
ty - reo tahiti
tyv - тыва дыл
tzm - ⵜⴰⵎⴰⵣⵉⵖⵜ
udm - удмурт
ug - ئۇيغۇرچە / Uyghurche
ug-arab - ئۇيغۇرچە
ug-latn - Uyghurche
uk - українська
ur - اردو
uz - oʻzbekcha / ўзбекча
uz-cyrl - ўзбекча
uz-latn - oʻzbekcha
ve - Tshivenda
vec - vèneto
vep - vepsän kel’
vi - Tiếng Việt
vls - West-Vlams
vmf - Mainfränkisch
vmw - emakhuwa
vo - Volapük
vot - Vaďďa
vro - võro
wa - walon
wal - wolaytta
war - Winaray
wls - Fakaʻuvea
wo - Wolof
wuu - 吴语
wuu-hans - 吴语(简体)
wuu-hant - 吳語(正體)
xal - хальмг
xh - isiXhosa
xmf - მარგალური
xsy - saisiyat
yi - ייִדיש
yo - Yorùbá
yrl - Nhẽẽgatú
yue - 粵語
yue-hans - 粵语(简体)
yue-hant - 粵語(繁體)
za - Vahcuengh
zea - Zeêuws
zgh - ⵜⴰⵎⴰⵣⵉⵖⵜ ⵜⴰⵏⴰⵡⴰⵢⵜ
zgh-latn - tamaziɣt tanawayt
zh - 中文
zh-cn - 中文(中国大陆)
zh-hans - 中文(简体)
zh-hant - 中文(繁體)
zh-hk - 中文(香港)
zh-mo - 中文(澳門)
zh-my - 中文(马来西亚)
zh-sg - 中文(新加坡)
zh-tw - 中文(臺灣)
zu - isiZulu
Format
Export for off-line translation
Export in native format
Export in CSV format
Fetch
<languages /> <div lang="en" dir="ltr" class="mw-content-ltr"> {{Cleanup}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> '''Nix flakes''' are an [[Experimental Nix features|experimental feature]] first introduced in the 2.4 [[Nix]] release,{{Cite manual|nix|development/experimental-features|number=13.8|title=Experimental Features|subsection=xp-feature-flakes|subtitle=flakes}}{{Cite manual|nix|release-notes/rl-2.4|number=14.27|title=Release 2.4 (2021-11-01)}} aiming to address a number of areas of improvement for the Nix ecosystem: they provide a uniform structure for Nix projects, allow for pinning specific versions of each dependencies, and sharing these dependencies via lock files, and overall make it more convenient to write reproducible Nix expressions. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> A flake is a directory which directly contains a Nix file called <code>flake.nix</code>, that follows a very specific structure. Flakes introduce a URL-like syntax{{Cite manual|nix|command-ref/new-cli/nix3-flake|number=8.5.17|title=nix flake|subsection=url-like-syntax|subtitle=URL-like syntax}} for specifying remote resources. To simplify the URL syntax, flakes use a registry of symbolic identifiers,{{Cite manual|nix|command-ref/new-cli/nix3-registry|number=8.5.62|title=nix registry}} allowing the direct specification of resources through syntax such as <code>github:NixOS/nixpkgs</code>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Flakes also allow for locking references and versions, which can then be queried and updated programatically via the inputs {{cite manual|nix|command-ref/new-cli/nix3-flake-lock|number=7.5.19|title=nix flake lock}}{{cite manual|nix|command-ref/new-cli/nix3-flake-info|number=7.5.17|title=nix flake info}}. Additionally, an experimental CLI utility accepts flake references for expressions that build, run, and deploy packages.{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Flake file structure == Minimally, a flake file contains a description of the flake, a set of input dependencies and an output. You can generate a very basic flake file at any time using nix flake init. This will populate the current directory with a file called flake.nix that will contain something akin to: {{File|3=<nowiki>{ description = "A very basic flake"; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; }; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> outputs = { self, nixpkgs }: { </div> <div lang="en" dir="ltr" class="mw-content-ltr"> packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> packages.x86_64-linux.default = self.packages.x86_64-linux.hello; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> }; }</nowiki>|name=flake.nix|lang=nix}} In the example above, you can see the description, the input specified as a GitHub repository with a specific branch (here <code>nixos/nixpkgs</code> on the <code>nixos-unstable</code> branch), and an output that makes use of the input. The output simply specifies that the flake contains one package for the x86_64 architecture called <code>hello</code>. Even if your flake's output wouldn't use its input (however, in practice, that is highly unlikely), the output still needs to be a Nix function. {{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Nix configuration === It is possible to override the global Nix configuration set in your <code>nix.conf</code> file for the purposes of evaluating a flake. This can be useful, for example, for setting up binary caches specific to certain projects, while keeping the global configuration untouched. The flake file can contain a nixConfig attribute with any relevant configuration settings supplied. For example, enabling the nix-community binary cache would be achieved by: {{File|3=<nowiki>{ ... nixConfig = { extra-substituters = [ "https://nix-community.cachix.org" ]; extra-trusted-public-keys = [ "nix-community.cachix.org-1:...=" ]; } }</nowiki>|name=flake.nix|lang=nix}}{{Note|If you are used to configuring your Nix settings via the NixOS configuration, these options are under <code>nix.settings</code> and not <code>nix</code>. For example, you cannot specify the automatic storage optimisation under <code>nix.optimisation.enable</code>.}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Setup == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Enabling flakes temporarily === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> When using any [[Nix command|<code>nix</code> command]], add the following command-line options: </div> <syntaxhighlight lang="shell"> --experimental-features 'nix-command flakes' </syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> === Enabling flakes permanently === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> ==== NixOS ==== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Add the following to the [[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration |NixOS configuration]]: </div> <syntaxHighlight lang=nix> nix.settings.experimental-features = [ "nix-command" "flakes" ]; </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> ====Home Manager==== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Add the following to your [[Home Manager|home manager]] config: </div> <syntaxhighlight lang="nix"> nix.settings.experimental-features = [ "nix-command" "flakes" ]; </syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> ====Nix standalone==== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> {{Note | The [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default.}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Add the following to <code>~/.config/nix/nix.conf</code> or <code>/etc/nix/nix.conf</code>: </div> <syntaxHighlight lang=text> experimental-features = nix-command flakes </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> == Usage == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> {{Warning | Since contents of flake files are copied to the world-readable [[Nix_package_manager#Nix_store|Nix store]] folder, do not put any unencrypted secrets in flake files. You should instead use a [[Comparison of secret managing schemes|secret managing scheme]].}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> {{Note | For flakes in [[git]] repositories, only files in the working tree will be copied to the store. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === The nix flakes command === {{Main|Nix (command)}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> The {{ic|nix flake}} subcommand is described in {{Nix Manual|name=command reference page of the Nix manual|anchor=command-ref/new-cli/nix3-flake}}. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> This flake produces a single flake output <code>packages</code>. And within that, <code>x86_64-linux</code> is a system-specifc attribute set. And within that, two package [[derivations]] <code>default</code> and <code>hello</code>. You can find outputs with the {{Nix Manual|name=show command|anchor=command-ref/new-cli/nix3-flake-show}} of a flake as shown below: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxhighlight lang="console"> $ nix flake show └───packages └───x86_64-linux ├───default: package 'hello-2.12.2' └───hello: package 'hello-2.12.2' </syntaxhighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> ==== Development shells ==== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> A <code>devShell</code> is a Nix-provided [[Development_environment_with_nix-shell#nix develop|development environment]] defined within a flake. It lets you declare a reproducible shell environment with the tools, libraries, and environment variables you need for the development of a specific project. This is flake equivalent to defining a <code>nix-shell</code>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxhighlight lang="nix"> { description = "Example flake with a devShell"; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> outputs = { self, nixpkgs}: let system = "x86_64-linux"; pkgs = import nixpkgs { inherit system; }; in { devShells.x86_64-linux.default = pkgs.mkShell { buildInputs = with pkgs; [ hello ]; shellHook = '' echo "Welcome to the devShell!" ''; }; }; } </syntaxhighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> To enter the development shell environment: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxhighlight lang="console"> $ nix develop </syntaxhighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> {{note|You don’t need to define a devShell to enter a development shell using nix develop. If no devShell is defined, nix develop will drop you into an environment containing the default build dependencies of the flake (if any).}} </div> <div lang="en" dir="ltr" class="mw-content-ltr"> ==== Build specific attributes in a flake repository ==== </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Running <code>nix build</code> will look in the <code>legacyPackages</code> and <code>packages</code> output attributes for the corresponding [[derivation]] and then your system architecture and build the default output. If you want to specify a build attribute in a flake repository, you can run <code>nix build .#<attr></code>. In the example above, if you wanted to build the <code>packages.x86_64-linux.hello</code> attribute, run: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxHighlight lang=console> $ nix build .#hello </syntaxHighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Likewise, you can specify an attribute with the run command: <code>nix run .#hello</code> and the develop command: <code>nix develop .#hello</code>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Flake schema == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> The flake.nix file is a Nix file but that has special restrictions (more on that later). </div> <div lang="en" dir="ltr" class="mw-content-ltr"> It has 4 top-level attributes: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code>description</code> is a string describing the flake. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code>inputs</code> is an attribute set of all the dependencies of the flake. The schema is described below. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code>outputs</code> is a function of one argument that takes an attribute set of all the realized inputs, and outputs another attribute set whose schema is described below. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code>nixConfig</code> is an attribute set of values which reflect the [https://nixos.org/manual/nix/stable/command-ref/conf-file.html values given to nix.conf]. This can extend the normal behavior of a user's nix experience by adding flake-specific configuration, such as a [[Binary Cache|binary cache]]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Input schema === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-inputs The nix flake inputs manual]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references The nix flake references manual]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> The inputs attribute defines the dependencies of the flake. For example, nixpkgs has to be defined as a dependency for a system flake in order for the system to build properly. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> [[Nixpkgs]] can be defined using the following code: </div> <code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> Nixpkgs can alternatively also point to an url cached by the NixOS organization: <code>inputs.nixpkgs.url = "<nowiki>https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz</nowiki>";</code> In this example the input would point to the `nixpkgs-unstable` channel. <div lang="en" dir="ltr" class="mw-content-ltr"> For any repository with its own flake.nix file, the website must also be defined. Nix knows where the nixpkgs repository is, so stating that it's on GitHub is unnecessary. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> For example, adding [[Hyprland]] as an input would look something like this: </div> <code>inputs.hyprland.url = "github:hyprwm/Hyprland";</code> <div lang="en" dir="ltr" class="mw-content-ltr"> If you want to make Hyprland follow the nixpkgs input to avoid having multiple versions of nixpkgs, this can be done using the following code: </div> <code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code> <div lang="en" dir="ltr" class="mw-content-ltr"> Using curly brackets({}), we can shorten all of this and put it in a table. The code will look something like this: </div> <syntaxhighlight lang="nix"> inputs = { nixpkgs.url = "github:NixOS/nixpkgs/<branch name>"; hyprland = { url = "github:hyprwm/Hyprland"; inputs.nixpkgs.follows = "nixpkgs"; }; }; </syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> By default, Git submodules in package <code>src</code>'s won't get copied to the nix store, this may cause the build to fail. Flakes in Git repositories can declare that they need Git submodules to be enabled. Since Nix version [https://discourse.nixos.org/t/nix-2-27-0-released/62003 2.27], you can enable submodules by: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxhighlight lang="nix"> inputs.self.submodules = true; </syntaxhighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Output schema === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> This is described in the nix package manager [https://github.com/NixOS/nix/blob/master/src/nix/flake-check.md src/nix/flake-check.md]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Once the inputs are resolved, they're passed to the function `outputs` along with with `self`, which is the directory of this flake in the store. `outputs` returns the outputs of the flake, according to the following schema. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Where: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code><system></code> is something like "x86_64-linux", "aarch64-linux", "i686-linux", "x86_64-darwin" </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code><name></code> is an attribute name like "hello". </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code><flake></code> is a flake name like "nixpkgs". </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code><store-path></code> is a <code>/nix/store..</code> path </div> <syntaxHighlight lang=nix> { self, ... }@inputs: { # Executed by `nix flake check` checks."<system>"."<name>" = derivation; # Executed by `nix build .#<name>` packages."<system>"."<name>" = derivation; # Executed by `nix build .` packages."<system>".default = derivation; # Executed by `nix run .#<name>` apps."<system>"."<name>" = { type = "app"; program = "<store-path>"; }; # Executed by `nix run . -- <args?>` apps."<system>".default = { type = "app"; program = "..."; }; # Formatter (alejandra, nixfmt or nixpkgs-fmt) formatter."<system>" = derivation; # Used for nixpkgs packages, also accessible via `nix build .#<name>` legacyPackages."<system>"."<name>" = derivation; # Overlay, consumed by other flakes overlays."<name>" = final: prev: { }; # Default overlay overlays.default = final: prev: { }; # Nixos module, consumed by other flakes nixosModules."<name>" = { config, ... }: { options = {}; config = {}; }; # Default module nixosModules.default = { config, ... }: { options = {}; config = {}; }; # Used with `nixos-rebuild switch --flake .#<hostname>` # nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation nixosConfigurations."<hostname>" = {}; # Used by `nix develop .#<name>` devShells."<system>"."<name>" = derivation; # Used by `nix develop` devShells."<system>".default = derivation; # Hydra build jobs hydraJobs."<attr>"."<system>" = derivation; # Used by `nix flake init -t <flake>#<name>` templates."<name>" = { path = "<store-path>"; description = "template description goes here?"; }; # Used by `nix flake init -t <flake>` templates.default = { path = "<store-path>"; description = ""; }; } </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> You can also define additional arbitrary attributes, but these are the outputs that Nix knows about. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Core usage patterns == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Making your evaluations pure === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Nix flakes are evaluated in a pure evaluation mode, meaning that access to the external environment is restricted to ensure reproducibility. To maintain purity when working with flakes, consider the following: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * {{Nixpkgs Manual|name=fetchurl|anchor=#sec-pkgs-fetchers-fetchurl-inputs}} and {{Nixpkgs Manual|name=fetchzip|anchor=#sec-pkgs-fetchers-fetchzip-inputs}} require a <code>sha256</code> argument to be considered pure. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code>builtins.currentSystem</code> is non-hermetic and impure as it reflects the host system performing the evauluation. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * <code>builtins.getEnv</code> is also impure. Avoid reading from environment variables and likewise, do not reference files outside of the flake's directory. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Defining a flake for multiple architectures === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Flakes force you to specify a program for each supported architecture. An example below shows how to write a flake that targets multiple architectures. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxhighlight lang="nix"> { description = "A flake targeting multiple architectures"; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; }; </div> <div lang="en" dir="ltr" class="mw-content-ltr"> outputs = { self, nixpkgs }: let systems = [ "x86_64-linux" "aarch64-linux" ]; forAllSystems = f: builtins.listToAttrs (map (system: { name = system; value = f system; }) systems); in { packages = forAllSystems (system: let pkgs = nixpkgs.legacyPackages.${system}; in { hello = pkgs.hello; default = pkgs.hello; }); }; } </syntaxhighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> You can also use third-parties projects like [[Flake Utils|flake-utils]] or [[Flake Parts|flake-parts]] that automatically provide code to avoid this boilerplate. To avoid re-defining the program multiple times, refer to [[Flake Utils#Defining a flake for multiple architectures]] </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Using overlays === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> To use [[Overlays]] with flakes, refer to [[Overlays#In a Nix flake]] page. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Enable unfree software === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> To allow for [[Unfree software|unfree software]] in a flake project, you need to explicitly allow it by setting <code>config.allowUnree = true;</code> when importing Nixpkgs. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <syntaxhighlight lang="nix"> { inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; outputs = { self, nixpkgs, flake-compat }: let system = "x86_64-linux"; pkgs = import nixpkgs { inherit system; config.allowUnfree = true;}; in { ... }; } </syntaxhighlight> </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == NixOS configuration with flakes == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> It is possible to manage a [[NixOS]] system configuration using flakes, gaining the benefits of reproducible, declarative inputs and streamlined updates. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> For details and examples, see [[NixOS system configuration#Defining NixOS as a flake]]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Development tricks == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Automatically switch nix shells with direnv === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> It is possible to automatically activate different Nix shells when navigating between project directories by using [[Direnv]]. Additional Nix integration with Direnv can be achieved with [https://github.com/nix-community/nix-direnv nix-direnv]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Pushing Flakes to Cachix === </div> https://docs.cachix.org/pushing#flakes <div lang="en" dir="ltr" class="mw-content-ltr"> === Flake support in projects without flakes === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> The [https://github.com/edolstra/flake-compat flake-compat] library provides a compatibility layer that allows projects using traditional <code>default.nix</code> and <code>shell.nix</code> files to operate with flakes. For more details and usage examples, see the [[Flake Compat]] page. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Another project that allows consuming flakes from non-flake projects is [https://github.com/fricklerhandwerk/flake-inputs flake-inputs]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Accessing flakes from Nix expressions === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> If you want to access a flake from within a regular Nix expression on a system that has flakes enabled, you can use something like <code>(builtins.getFlake "/path/to/directory").packages.x86_64-linux.default</code>, where 'directory' is the directory that contains your <code>flake.nix</code>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Efficiently build multiple flake outputs === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> To push ''all'' flake outputs automatically, checkout [https://github.com/srid/devour-flake#usage devour-flake]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Build a package added in a PR === </div> <syntaxHighlight> nix build github:nixos/nixpkgs?ref=pull/<PR_NUMBER>/head#<PACKAGE> </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> this allows building a package that has not yet been added to nixpkgs. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> note that this will download a full source tarball of nixpkgs. if you already have a local clone, using that may be faster due to delta compression: </div> <syntaxHighlight> git fetch upstream pull/<PR_NUMBER>/head && git checkout FETCH_HEAD && nix build .#PACKAGE </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> this allows building a package that has not yet been added to nixpkgs. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === How to add a file locally in git but not include it in commits === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> When a [[git]] folder exists, flake will only copy files added in git to maximize reproducibility (this way if you forgot to add a local file in your repo, you will directly get an error when you try to compile it). However, for development purpose you may want to create an alternative flake file, for instance containing configuration for your preferred editors as described [https://discourse.nixos.org/t/local-personal-development-tools-with-flakes/22714/8 here]… of course without committing this file since it contains only your own preferred tools. You can do so by doing something like that (say for a file called <code>extra/flake.nix</code>): </div> <syntaxHighlight> git add --intent-to-add extra/flake.nix git update-index --skip-worktree --assume-unchanged extra/flake.nix </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> === Rapid iteration of a direct dependency === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> One common pain point with using Nix as a development environment is the need to completely rebuild dependencies and re-enter the dev shell every time they are updated. The <code>nix develop --redirect <flake> <directory></code> command allows you to provide a mutable dependency to your shell as if it were built by Nix. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Consider a situation where your executable, <code>consumexe</code>, depends on a library, <code>libdep</code>. You're trying to work on both at the same time, where changes to <code>libdep</code> are reflected in real time for <code>consumexe</code>. This workflow can be achieved like so: </div> <syntaxHighlight lang=bash> cd ~/libdep-src-checkout/ nix develop # Or `nix-shell` if applicable. export prefix="./install" # configure nix to install it here buildPhase # build it like nix does installPhase # install it like nix does </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> Now that you've built the dependency, <code>consumexe</code> can take it as an input. '''In another terminal''': </div> <syntaxHighlight lang=bash> cd ~/consumexe-src-checkout/ nix develop --redirect libdep ~/libdep-src-checkout/install echo $buildInputs | tr " " "\n" | grep libdep # Output should show ~/libdep-src-checkout/ so you know it worked </syntaxHighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> If Nix warns you that your redirected flake isn't actually used as an input to the evaluated flake, try using the <code>--inputs-from .</code> flag. If all worked well you should be able to <code>buildPhase && installPhase</code> when the dependency changes and rebuild your consumer with the new version ''without'' exiting the development shell. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == See also == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Official sources === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://nix.dev/concepts/flakes Flakes] - nix.dev </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html Nix flake command reference manual] - Many additional details about flakes, and their parts. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://github.com/NixOS/nix/blob/master/src/nix/flake.md spec describing flake inputs in more detail] </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://github.com/NixOS/rfcs/pull/49 RFC 49] (2019) - Original flakes specification </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Guides === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://jade.fyi/blog/flakes-arent-real/ Flakes aren't real and can't hurt you] (Jade Lovelace, 2024) </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book](Ryan4yin, 2023) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://xeiaso.net/blog/nix-flakes-1-2022-02-21 Nix Flakes: an Introduction] (Xe Iaso, 2022) </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://serokell.io/blog/practical-nix-flakes Practical Nix Flakes] (Alexander Bantyev, 2021) - Intro article on working with Nix and Flakes </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://www.tweag.io/blog/2020-05-25-flakes/ Nix Flakes, Part 1: An introduction and tutorial] (Eelco Dolstra, 2020) </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://www.tweag.io/blog/2020-06-25-eval-cache/ Nix Flakes, Part 2: Evaluation caching] (Eelco Dolstra, 2020) </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://www.tweag.io/blog/2020-07-31-nixos-flakes/ Nix Flakes, Part 3: Managing NixOS systems] (Eelco Dolstra, 2020) </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020) YouTube video </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Useful flake modules === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [[Flake Utils|flake-utils]]: Library to avoid some boiler-code when writing flakes </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [[Flake Compat|flake-compat]]: A compatibility layer for flakes </div> <div lang="en" dir="ltr" class="mw-content-ltr"> * [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] </div> <div lang="en" dir="ltr" class="mw-content-ltr"> {{references}} </div> [[Category:Software]] [[Category:Nix]] [[Category:Nix Language]] [[Category:Flakes]]