Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

547 строки
16 KiB

  1. <chapter xmlns="http://docbook.org/ns/docbook"
  2. xmlns:xlink="http://www.w3.org/1999/xlink"
  3. xml:id="chap-packageconfig">
  4. <title>Global configuration</title>
  5. <para>
  6. Nix comes with certain defaults about what packages can and cannot be
  7. installed, based on a package's metadata. By default, Nix will prevent
  8. installation if any of the following criteria are true:
  9. </para>
  10. <itemizedlist>
  11. <listitem>
  12. <para>
  13. The package is thought to be broken, and has had its
  14. <literal>meta.broken</literal> set to <literal>true</literal>.
  15. </para>
  16. </listitem>
  17. <listitem>
  18. <para>
  19. The package isn't intended to run on the given system, as none of its
  20. <literal>meta.platforms</literal> match the given system.
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. The package's <literal>meta.license</literal> is set to a license which is
  26. considered to be unfree.
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. The package has known security vulnerabilities but has not or can not be
  32. updated for some reason, and a list of issues has been entered in to the
  33. package's <literal>meta.knownVulnerabilities</literal>.
  34. </para>
  35. </listitem>
  36. </itemizedlist>
  37. <para>
  38. Note that all this is checked during evaluation already, and the check
  39. includes any package that is evaluated. In particular, all build-time
  40. dependencies are checked. <literal>nix-env -qa</literal> will (attempt to)
  41. hide any packages that would be refused.
  42. </para>
  43. <para>
  44. Each of these criteria can be altered in the nixpkgs configuration.
  45. </para>
  46. <para>
  47. The nixpkgs configuration for a NixOS system is set in the
  48. <literal>configuration.nix</literal>, as in the following example:
  49. <programlisting>
  50. {
  51. nixpkgs.config = {
  52. allowUnfree = true;
  53. };
  54. }
  55. </programlisting>
  56. However, this does not allow unfree software for individual users. Their
  57. configurations are managed separately.
  58. </para>
  59. <para>
  60. A user's of nixpkgs configuration is stored in a user-specific configuration
  61. file located at <filename>~/.config/nixpkgs/config.nix</filename>. For
  62. example:
  63. <programlisting>
  64. {
  65. allowUnfree = true;
  66. }
  67. </programlisting>
  68. </para>
  69. <para>
  70. Note that we are not able to test or build unfree software on Hydra due to
  71. policy. Most unfree licenses prohibit us from either executing or
  72. distributing the software.
  73. </para>
  74. <section xml:id="sec-allow-broken">
  75. <title>Installing broken packages</title>
  76. <para>
  77. There are two ways to try compiling a package which has been marked as
  78. broken.
  79. </para>
  80. <itemizedlist>
  81. <listitem>
  82. <para>
  83. For allowing the build of a broken package once, you can use an
  84. environment variable for a single invocation of the nix tools:
  85. <programlisting>$ export NIXPKGS_ALLOW_BROKEN=1</programlisting>
  86. </para>
  87. </listitem>
  88. <listitem>
  89. <para>
  90. For permanently allowing broken packages to be built, you may add
  91. <literal>allowBroken = true;</literal> to your user's configuration file,
  92. like this:
  93. <programlisting>
  94. {
  95. allowBroken = true;
  96. }
  97. </programlisting>
  98. </para>
  99. </listitem>
  100. </itemizedlist>
  101. </section>
  102. <section xml:id="sec-allow-unsupported-system">
  103. <title>Installing packages on unsupported systems</title>
  104. <para>
  105. There are also two ways to try compiling a package which has been marked as
  106. unsuported for the given system.
  107. </para>
  108. <itemizedlist>
  109. <listitem>
  110. <para>
  111. For allowing the build of a broken package once, you can use an
  112. environment variable for a single invocation of the nix tools:
  113. <programlisting>$ export NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1</programlisting>
  114. </para>
  115. </listitem>
  116. <listitem>
  117. <para>
  118. For permanently allowing broken packages to be built, you may add
  119. <literal>allowUnsupportedSystem = true;</literal> to your user's
  120. configuration file, like this:
  121. <programlisting>
  122. {
  123. allowUnsupportedSystem = true;
  124. }
  125. </programlisting>
  126. </para>
  127. </listitem>
  128. </itemizedlist>
  129. <para>
  130. The difference between a package being unsupported on some system and
  131. being broken is admittedly a bit fuzzy. If a program
  132. <emphasis>ought</emphasis> to work on a certain platform, but doesn't, the
  133. platform should be included in <literal>meta.platforms</literal>, but marked
  134. as broken with e.g. <literal>meta.broken =
  135. !hostPlatform.isWindows</literal>. Of course, this begs the question of what
  136. "ought" means exactly. That is left to the package maintainer.
  137. </para>
  138. </section>
  139. <section xml:id="sec-allow-unfree">
  140. <title>Installing unfree packages</title>
  141. <para>
  142. There are several ways to tweak how Nix handles a package which has been
  143. marked as unfree.
  144. </para>
  145. <itemizedlist>
  146. <listitem>
  147. <para>
  148. To temporarily allow all unfree packages, you can use an environment
  149. variable for a single invocation of the nix tools:
  150. <programlisting>$ export NIXPKGS_ALLOW_UNFREE=1</programlisting>
  151. </para>
  152. </listitem>
  153. <listitem>
  154. <para>
  155. It is possible to permanently allow individual unfree packages, while
  156. still blocking unfree packages by default using the
  157. <literal>allowUnfreePredicate</literal> configuration option in the user
  158. configuration file.
  159. </para>
  160. <para>
  161. This option is a function which accepts a package as a parameter, and
  162. returns a boolean. The following example configuration accepts a package
  163. and always returns false:
  164. <programlisting>
  165. {
  166. allowUnfreePredicate = (pkg: false);
  167. }
  168. </programlisting>
  169. </para>
  170. <para>
  171. For a more useful example, try the following. This configuration
  172. only allows unfree packages named flash player and visual studio
  173. code:
  174. <programlisting>
  175. {
  176. allowUnfreePredicate = (pkg: builtins.elem
  177. (builtins.parseDrvName pkg.name).name [
  178. "flashplayer"
  179. "vscode"
  180. ]);
  181. }
  182. </programlisting>
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. It is also possible to whitelist and blacklist licenses that are
  188. specifically acceptable or not acceptable, using
  189. <literal>whitelistedLicenses</literal> and
  190. <literal>blacklistedLicenses</literal>, respectively.
  191. </para>
  192. <para>
  193. The following example configuration whitelists the licenses
  194. <literal>amd</literal> and <literal>wtfpl</literal>:
  195. <programlisting>
  196. {
  197. whitelistedLicenses = with stdenv.lib.licenses; [ amd wtfpl ];
  198. }
  199. </programlisting>
  200. </para>
  201. <para>
  202. The following example configuration blacklists the <literal>gpl3</literal>
  203. and <literal>agpl3</literal> licenses:
  204. <programlisting>
  205. {
  206. blacklistedLicenses = with stdenv.lib.licenses; [ agpl3 gpl3 ];
  207. }
  208. </programlisting>
  209. </para>
  210. </listitem>
  211. </itemizedlist>
  212. <para>
  213. A complete list of licenses can be found in the file
  214. <filename>lib/licenses.nix</filename> of the nixpkgs tree.
  215. </para>
  216. </section>
  217. <section xml:id="sec-allow-insecure">
  218. <title>Installing insecure packages</title>
  219. <para>
  220. There are several ways to tweak how Nix handles a package which has been
  221. marked as insecure.
  222. </para>
  223. <itemizedlist>
  224. <listitem>
  225. <para>
  226. To temporarily allow all insecure packages, you can use an environment
  227. variable for a single invocation of the nix tools:
  228. <programlisting>$ export NIXPKGS_ALLOW_INSECURE=1</programlisting>
  229. </para>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. It is possible to permanently allow individual insecure packages, while
  234. still blocking other insecure packages by default using the
  235. <literal>permittedInsecurePackages</literal> configuration option in the
  236. user configuration file.
  237. </para>
  238. <para>
  239. The following example configuration permits the installation of the
  240. hypothetically insecure package <literal>hello</literal>, version
  241. <literal>1.2.3</literal>:
  242. <programlisting>
  243. {
  244. permittedInsecurePackages = [
  245. "hello-1.2.3"
  246. ];
  247. }
  248. </programlisting>
  249. </para>
  250. </listitem>
  251. <listitem>
  252. <para>
  253. It is also possible to create a custom policy around which insecure
  254. packages to allow and deny, by overriding the
  255. <literal>allowInsecurePredicate</literal> configuration option.
  256. </para>
  257. <para>
  258. The <literal>allowInsecurePredicate</literal> option is a function which
  259. accepts a package and returns a boolean, much like
  260. <literal>allowUnfreePredicate</literal>.
  261. </para>
  262. <para>
  263. The following configuration example only allows insecure packages with
  264. very short names:
  265. <programlisting>
  266. {
  267. allowInsecurePredicate = (pkg: (builtins.stringLength (builtins.parseDrvName pkg.name).name) &lt;= 5);
  268. }
  269. </programlisting>
  270. </para>
  271. <para>
  272. Note that <literal>permittedInsecurePackages</literal> is only checked if
  273. <literal>allowInsecurePredicate</literal> is not specified.
  274. </para>
  275. </listitem>
  276. </itemizedlist>
  277. </section>
  278. <!--============================================================-->
  279. <section xml:id="sec-modify-via-packageOverrides">
  280. <title>Modify packages via <literal>packageOverrides</literal></title>
  281. <para>
  282. You can define a function called <varname>packageOverrides</varname> in your
  283. local <filename>~/.config/nixpkgs/config.nix</filename> to override Nix
  284. packages. It must be a function that takes pkgs as an argument and returns a
  285. modified set of packages.
  286. <programlisting>
  287. {
  288. packageOverrides = pkgs: rec {
  289. foo = pkgs.foo.override { ... };
  290. };
  291. }
  292. </programlisting>
  293. </para>
  294. </section>
  295. <section xml:id="sec-declarative-package-management">
  296. <title>Declarative Package Management</title>
  297. <section xml:id="sec-building-environment">
  298. <title>Build an environment</title>
  299. <para>
  300. Using <literal>packageOverrides</literal>, it is possible to manage
  301. packages declaratively. This means that we can list all of our desired
  302. packages within a declarative Nix expression. For example, to have
  303. <literal>aspell</literal>, <literal>bc</literal>,
  304. <literal>ffmpeg</literal>, <literal>coreutils</literal>,
  305. <literal>gdb</literal>, <literal>nixUnstable</literal>,
  306. <literal>emscripten</literal>, <literal>jq</literal>,
  307. <literal>nox</literal>, and <literal>silver-searcher</literal>, we could
  308. use the following in <filename>~/.config/nixpkgs/config.nix</filename>:
  309. </para>
  310. <screen>
  311. {
  312. packageOverrides = pkgs: with pkgs; {
  313. myPackages = pkgs.buildEnv {
  314. name = "my-packages";
  315. paths = [
  316. aspell
  317. bc
  318. coreutils
  319. gdb
  320. ffmpeg
  321. nixUnstable
  322. emscripten
  323. jq
  324. nox
  325. silver-searcher
  326. ];
  327. };
  328. };
  329. }
  330. </screen>
  331. <para>
  332. To install it into our environment, you can just run <literal>nix-env -iA
  333. nixpkgs.myPackages</literal>. If you want to load the packages to be built
  334. from a working copy of <literal>nixpkgs</literal> you just run
  335. <literal>nix-env -f. -iA myPackages</literal>. To explore what's been
  336. installed, just look through <filename>~/.nix-profile/</filename>. You can
  337. see that a lot of stuff has been installed. Some of this stuff is useful
  338. some of it isn't. Let's tell Nixpkgs to only link the stuff that we want:
  339. </para>
  340. <screen>
  341. {
  342. packageOverrides = pkgs: with pkgs; {
  343. myPackages = pkgs.buildEnv {
  344. name = "my-packages";
  345. paths = [
  346. aspell
  347. bc
  348. coreutils
  349. gdb
  350. ffmpeg
  351. nixUnstable
  352. emscripten
  353. jq
  354. nox
  355. silver-searcher
  356. ];
  357. pathsToLink = [ "/share" "/bin" ];
  358. };
  359. };
  360. }
  361. </screen>
  362. <para>
  363. <literal>pathsToLink</literal> tells Nixpkgs to only link the paths listed
  364. which gets rid of the extra stuff in the profile. <filename>/bin</filename>
  365. and <filename>/share</filename> are good defaults for a user environment,
  366. getting rid of the clutter. If you are running on Nix on MacOS, you may
  367. want to add another path as well, <filename>/Applications</filename>, that
  368. makes GUI apps available.
  369. </para>
  370. </section>
  371. <section xml:id="sec-getting-documentation">
  372. <title>Getting documentation</title>
  373. <para>
  374. After building that new environment, look through
  375. <filename>~/.nix-profile</filename> to make sure everything is there that
  376. we wanted. Discerning readers will note that some files are missing. Look
  377. inside <filename>~/.nix-profile/share/man/man1/</filename> to verify this.
  378. There are no man pages for any of the Nix tools! This is because some
  379. packages like Nix have multiple outputs for things like documentation (see
  380. section 4). Let's make Nix install those as well.
  381. </para>
  382. <screen>
  383. {
  384. packageOverrides = pkgs: with pkgs; {
  385. myPackages = pkgs.buildEnv {
  386. name = "my-packages";
  387. paths = [
  388. aspell
  389. bc
  390. coreutils
  391. ffmpeg
  392. nixUnstable
  393. emscripten
  394. jq
  395. nox
  396. silver-searcher
  397. ];
  398. pathsToLink = [ "/share/man" "/share/doc" "/bin" ];
  399. extraOutputsToInstall = [ "man" "doc" ];
  400. };
  401. };
  402. }
  403. </screen>
  404. <para>
  405. This provides us with some useful documentation for using our packages.
  406. However, if we actually want those manpages to be detected by man, we need
  407. to set up our environment. This can also be managed within Nix expressions.
  408. </para>
  409. <screen>
  410. {
  411. packageOverrides = pkgs: with pkgs; rec {
  412. myProfile = writeText "my-profile" ''
  413. export PATH=$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
  414. export MANPATH=$HOME/.nix-profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
  415. '';
  416. myPackages = pkgs.buildEnv {
  417. name = "my-packages";
  418. paths = [
  419. (runCommand "profile" {} ''
  420. mkdir -p $out/etc/profile.d
  421. cp ${myProfile} $out/etc/profile.d/my-profile.sh
  422. '')
  423. aspell
  424. bc
  425. coreutils
  426. ffmpeg
  427. man
  428. nixUnstable
  429. emscripten
  430. jq
  431. nox
  432. silver-searcher
  433. ];
  434. pathsToLink = [ "/share/man" "/share/doc" "/bin" "/etc" ];
  435. extraOutputsToInstall = [ "man" "doc" ];
  436. };
  437. };
  438. }
  439. </screen>
  440. <para>
  441. For this to work fully, you must also have this script sourced when you are
  442. logged in. Try adding something like this to your
  443. <filename>~/.profile</filename> file:
  444. </para>
  445. <screen>
  446. #!/bin/sh
  447. if [ -d $HOME/.nix-profile/etc/profile.d ]; then
  448. for i in $HOME/.nix-profile/etc/profile.d/*.sh; do
  449. if [ -r $i ]; then
  450. . $i
  451. fi
  452. done
  453. fi
  454. </screen>
  455. <para>
  456. Now just run <literal>source $HOME/.profile</literal> and you can starting
  457. loading man pages from your environent.
  458. </para>
  459. </section>
  460. <section xml:id="sec-gnu-info-setup">
  461. <title>GNU info setup</title>
  462. <para>
  463. Configuring GNU info is a little bit trickier than man pages. To work
  464. correctly, info needs a database to be generated. This can be done with
  465. some small modifications to our environment scripts.
  466. </para>
  467. <screen>
  468. {
  469. packageOverrides = pkgs: with pkgs; rec {
  470. myProfile = writeText "my-profile" ''
  471. export PATH=$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
  472. export MANPATH=$HOME/.nix-profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
  473. export INFOPATH=$HOME/.nix-profile/share/info:/nix/var/nix/profiles/default/share/info:/usr/share/info
  474. '';
  475. myPackages = pkgs.buildEnv {
  476. name = "my-packages";
  477. paths = [
  478. (runCommand "profile" {} ''
  479. mkdir -p $out/etc/profile.d
  480. cp ${myProfile} $out/etc/profile.d/my-profile.sh
  481. '')
  482. aspell
  483. bc
  484. coreutils
  485. ffmpeg
  486. man
  487. nixUnstable
  488. emscripten
  489. jq
  490. nox
  491. silver-searcher
  492. texinfoInteractive
  493. ];
  494. pathsToLink = [ "/share/man" "/share/doc" "/share/info" "/bin" "/etc" ];
  495. extraOutputsToInstall = [ "man" "doc" "info" ];
  496. postBuild = ''
  497. if [ -x $out/bin/install-info -a -w $out/share/info ]; then
  498. shopt -s nullglob
  499. for i in $out/share/info/*.info $out/share/info/*.info.gz; do
  500. $out/bin/install-info $i $out/share/info/dir
  501. done
  502. fi
  503. '';
  504. };
  505. };
  506. }
  507. </screen>
  508. <para>
  509. <literal>postBuild</literal> tells Nixpkgs to run a command after building
  510. the environment. In this case, <literal>install-info</literal> adds the
  511. installed info pages to <literal>dir</literal> which is GNU info's default
  512. root node. Note that <literal>texinfoInteractive</literal> is added to the
  513. environment to give the <literal>install-info</literal> command.
  514. </para>
  515. </section>
  516. </section>
  517. </chapter>