Jump to content

Nixpkgs/Building RPM DEB with nixpkgs: Difference between revisions

From Official NixOS Wiki
imported>Sikmir
No edit summary
Luflosi (talk | contribs)
Don't require cloning Nixpkgs; Also improve the capitalization of some words a bit
 
Line 12: Line 12:
To build deb/rpm packages with nix/nixpkgs, it is critical to understand the process. Hence a short intro:
To build deb/rpm packages with nix/nixpkgs, it is critical to understand the process. Hence a short intro:


To build a deb or rpm package with nixpkgs, the build process needs a ''disk image'' which it then uses to boot a virtual machine (as in Qemu). Inside this virtual machine, the deb/rpm package is compiled from a ''tarball'' and then packaged using the respective tools for deb/rpm packaging.
To build a deb or rpm package with Nixpkgs, the build process needs a ''disk image'' which it then uses to boot a virtual machine (as in QEMU). Inside this virtual machine, the deb/rpm package is compiled from a ''tarball'' and then packaged using the respective tools for deb/rpm packaging.


Luckily, all that requirements are shipped inside nixpkgs itself.
Luckily, all that requirements are shipped inside Nixpkgs itself.


== Preparation of the host machine ==
== Preparation of the host machine ==
Line 32: Line 32:




== How to build a deb/rpm with nixpkgs ==
== How to build a deb/rpm with Nixpkgs ==


The following approach can be used to try out building a <code>.deb</code> package with nixpkgs:
The following approach can be used to try out building a <code>.deb</code> package with Nixpkgs:


# Clone the official nixpkgs repository into <code>~/nixpkgs</code>
Create a file called <code>deb.nix</code> with the following contents:
# Add a file to the nixpkgs repository <code>./deb.nix</code> with the following contents:


(In this scenario, ncurses was built for testing the whole approach)
(In this scenario, we build ncurses for testing the whole approach)


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
let
let
   pkgs = (import ./default.nix {});
   pkgs = import <nixpkgs> { };
   vm   = pkgs.vmTools.diskImageFuns.centos7x86_64 {};
   vm = pkgs.vmTools.diskImageFuns.centos7x86_64 { };
   args = {
   args = {
     diskImage = vm;
     diskImage = vm;
     diskImageFormat = "qcow2";
     diskImageFormat = "qcow2";
     src       = pkgs.ncurses.src;
     src = pkgs.ncurses.src;
     name     = "ncurses-deb";
     name = "ncurses-deb";
     buildInputs = [];
     buildInputs = [ ];
     meta.description = "No descr";
     meta.description = "No descr";
   };
   };
in pkgs.releaseTools.debBuild args
in pkgs.releaseTools.debBuild args
</syntaxHighlight>
</syntaxhighlight>


and then this file was called: <code>nix-build ./deb.nix</code>.
and then build it: <code>nix-build ./deb.nix</code>.


This loads the relevant disk images from the channel mirror and spins up the Qemu VM to build the package.
This loads the relevant disk images from the channel mirror and spins up the QEMU VM to build the package.
The deb file (in this case, as we're building a deb with the expression from above) is located in ./result/debs/ after a successful build.
The deb file (in this case, as we're building a deb with the expression from above) is located in ./result/debs/ after a successful build.
Here, we're using the nixpkgs channel (<code><nixpkgs></code>) for convenience but in order to ensure you can build the same thing tomorrow, it is recommended to pin Nixpkgs to a specific Git commit instead.

Latest revision as of 07:50, 25 November 2026

☶︎
This article or section needs to be expanded. Further information may be found in the related discussion page. Please consult the pedia article metapage for guidelines on contributing.

Please note that this page is under construction and the provided information is incomplete. Reach out to matthiasbeyer if this page was not updated in a long time.



How it works

To build deb/rpm packages with nix/nixpkgs, it is critical to understand the process. Hence a short intro:

To build a deb or rpm package with Nixpkgs, the build process needs a disk image which it then uses to boot a virtual machine (as in QEMU). Inside this virtual machine, the deb/rpm package is compiled from a tarball and then packaged using the respective tools for deb/rpm packaging.

Luckily, all that requirements are shipped inside Nixpkgs itself.

Preparation of the host machine

The host machine has to be prepared for the build process.

In the evaluation for this article, a CentOS 7 amd64 machine running in vmware was used. On the machine, the nix package manager was installed via curl https://nixos.org/nix/install | sh as suggested on https://nixos.org/nix/ > "Get Nix".

The configuration file /etc/nix/nix.conf was edited for the following contents:

sandbox = false
system-features = [ kvm nixos-test benchmark big-parallel ]

This step might not be necessary on a NixOS installation.


How to build a deb/rpm with Nixpkgs

The following approach can be used to try out building a .deb package with Nixpkgs:

Create a file called deb.nix with the following contents:

(In this scenario, we build ncurses for testing the whole approach)

let
  pkgs = import <nixpkgs> { };
  vm = pkgs.vmTools.diskImageFuns.centos7x86_64 { };
  args = {
    diskImage = vm;
    diskImageFormat = "qcow2";
    src = pkgs.ncurses.src;
    name = "ncurses-deb";
    buildInputs = [ ];
    meta.description = "No descr";
  };
in pkgs.releaseTools.debBuild args

and then build it: nix-build ./deb.nix.

This loads the relevant disk images from the channel mirror and spins up the QEMU VM to build the package. The deb file (in this case, as we're building a deb with the expression from above) is located in ./result/debs/ after a successful build.

Here, we're using the nixpkgs channel (<nixpkgs>) for convenience but in order to ensure you can build the same thing tomorrow, it is recommended to pin Nixpkgs to a specific Git commit instead.