Flakes: Difference between revisions

imported>Asymmetric
m Non-NixOS: fix snippet to nixFlakes
imported>Wackbyte
m fix syntax highlighting error + missing syntax highlighting
Line 63: Line 63:
Edit either <code>~/.config/nix/nix.conf</code> or <code>/etc/nix/nix.conf</code> and add:
Edit either <code>~/.config/nix/nix.conf</code> or <code>/etc/nix/nix.conf</code> and add:


<syntaxHighlight>
<syntaxHighlight lang=text>
experimental-features = nix-command flakes  
experimental-features = nix-command flakes  
</syntaxHighlight>
</syntaxHighlight>
Line 357: Line 357:
With this in place, you can now replace the use nix invocation in the <code>.envrc</code> file with <code>use flake</code>:
With this in place, you can now replace the use nix invocation in the <code>.envrc</code> file with <code>use flake</code>:


<syntaxHighlight>
<syntaxHighlight lang=text>
# .envrc
# .envrc
use flake
use flake
Line 407: Line 407:
in nixpkgs.overlays = [ overlay-unstable ]; # we assign the overlay created before to the overlays of nixpkgs.
in nixpkgs.overlays = [ overlay-unstable ]; # we assign the overlay created before to the overlays of nixpkgs.
</syntaxHighlight>
</syntaxHighlight>
should make a package accessible through <syntaxHighlight>pkgs.unstable.package</syntaxHighlight>
should make a package accessible through <code>pkgs.unstable.package</code>
For example, a NixOS config flake skeleton could be as follows:
For example, a NixOS config flake skeleton could be as follows:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
Line 448: Line 448:
}  
}  
</syntaxHighlight>
</syntaxHighlight>
Same can be done with the NURs, as it already has an ''overlay'' attribute in the flake.nix of the project, you can just add <syntaxHighlight>nixpkgs.overlays = [ nur.overlay ];</syntaxHighlight>
Same can be done with the NURs, as it already has an ''overlay'' attribute in the flake.nix of the project, you can just add <syntaxHighlight lang=nix>nixpkgs.overlays = [ nur.overlay ];</syntaxHighlight>


If the variable <code>nixpkgs</code> points to the flake, you can also define <code>pkgs</code> with overlays with:
If the variable <code>nixpkgs</code> points to the flake, you can also define <code>pkgs</code> with overlays with:
Line 460: Line 460:
How to get a nix repl out of your system flake:
How to get a nix repl out of your system flake:


<syntaxHighlight>
<syntaxHighlight lang=text>
# nix repl  
# nix repl  
>> :lf /etc/nixos
>> :lf /etc/nixos
Line 466: Line 466:


Or out of your current flake:
Or out of your current flake:
<syntaxHighlight>
<syntaxHighlight lang=text>
# nix repl  
# nix repl  
>> :lf .#
>> :lf .#
Line 474: Line 474:
However, this won't be instant upon evaluation if any file changes have been done since your last configuration rebuild. Instead, if one puts:
However, this won't be instant upon evaluation if any file changes have been done since your last configuration rebuild. Instead, if one puts:


<syntaxHighlight>nix.nixPath = let path = toString ./.; in [ "repl=${path}/repl.nix" "nixpkgs=${inputs.nixpkgs}" ];</syntaxHighlight>
<syntaxHighlight lang=nix>
nix.nixPath = let path = toString ./.; in [ "repl=${path}/repl.nix" "nixpkgs=${inputs.nixpkgs}" ];
</syntaxHighlight>


In their system <code>flake.nix</code> configuration file, and includes the following file in their root directory flake as <code>repl.nix</code>:
In their system <code>flake.nix</code> configuration file, and includes the following file in their root directory flake as <code>repl.nix</code>:
Line 494: Line 496:
Then one can run (or bind a shell alias):
Then one can run (or bind a shell alias):


<syntaxHighlight>
<syntaxHighlight lang=bash>
source /etc/set-environment && nix repl $(echo $NIX_PATH | perl -pe 's|.*(/nix/store/.*-source/repl.nix).*|\1|')</syntaxHighlight>
source /etc/set-environment && nix repl $(echo $NIX_PATH | perl -pe 's|.*(/nix/store/.*-source/repl.nix).*|\1|')</syntaxHighlight>


Line 509: Line 511:


To use nonfree software in a flake, add <code>nixpkgs</code> as an input in your flake and import it with the <code>allowUnfree</code> option:
To use nonfree software in a flake, add <code>nixpkgs</code> as an input in your flake and import it with the <code>allowUnfree</code> option:
<syntaxHighlight>
<syntaxHighlight lang=nix>
pkgs = import nixpkgs { config = { allowUnfree = true; }; }
pkgs = import nixpkgs { config = { allowUnfree = true; }; };
</syntaxHighlight>
</syntaxHighlight>


Line 517: Line 519:
If you want to install software using home-manager via nix flakes in non NixOS systems (like darwin) you can use the home-manager <code>nixpkgs.config</code> option for example
If you want to install software using home-manager via nix flakes in non NixOS systems (like darwin) you can use the home-manager <code>nixpkgs.config</code> option for example


<pre>nixpkgs.config.allowUnfree = true;</pre>
<syntaxHighlight lang=nix>
nixpkgs.config.allowUnfree = true;
</syntaxHighlight>


== Official Nix links ==
== Official Nix links ==