How can I use Nix to statically compile the program in Application

I heard that Nix can be used to automatically and statically compile programs in the Application library, but after reading some of the instructions, I didn’t find out how to do it. What command should I use to automatically compile the program statically?

I assume when you say “Application library”, you mean nixpkgs?
Nix does not help you with static compilation of programs, as I already explained to @Axwiolv in the other thread.

nixStatic is just a statically linked binary of regular nix, it works exactly like regular Nix, meaning you build packages as a set of dependencies that are all stored in /nix/store. You can copy compiled programs to other machines using the nix copy command, but I’m not sure if that’s what you’re after. If you take a look at the nixStatic expression in nixpkgs, you’ll see that it uses very specific building instructions to build a statically linked binary. You’d have to do the same for every package you want to build this way.

Could you explain in more detail what it is you’re trying to achieve? What sort of applications do you want to build statically and why?

I’m also curious why two new users joined within the span of 1 hour and asked almost the exact same question in two different threads and a private message with similar wording.

Screenshot 2023-10-18 at 13.18.10
Screenshot 2023-10-18 at 13.18.24

Is @Axwiolv your account as well? What is going on here?

I want to use a static library to automatically statically link and compile programs in the Application library, because someone mentioned in one of my papers that nix can be used to automatically build. As for Axwioiv, it is another account of mine because I submitted one in the forum. This post resulted in me being unable to speak before review. I thought I was banned, so I created a new account.

Ah ok, no worries then.

Yes, one of the uses of Nix is as a meta-build-tool. You can write a Nix expression that builds a package, but how you do that depends on the language it is written in, as the Nix build process internally still calls other tools like make, meson, poetry, cargo and so on.

Again, if you want to build something statically, you need to specifically write a nix expression for that. If you do it right, you can just run nix build . and it will build everything you tell it to, and I will gladly help you with that, but it is not possible to just take a package from nixpkgs and say “please compile that but statically linked”, Nix does not provide you with that option, there are no built-in mechanisms for this.

For most usecases, Nix sidesteps the issue by putting everything in /nix/store and ensuring depedencies are linked directly and without conflict, so while everything is still linked “dynamically”, the dynamic loader can only discover a single shared library, the one specified at build time, which is as good as static compilation in terms of reproducibility and immutability.

Could you please clarify what “Application library” you’re talking about? Could you maybe link the paper you mentioned or share the message that told you about Nix so that I can understand what you’re usecase is? If you don’t want to share this info publicly, you can also answer in a private message again :slight_smile:

That paper is “ModX: Binary Level Partially Imported Third-Party Library
Detection via Program Modularization and Semantic Matching", nix is mentioned in this paragraph of the article

I asked the author and he said that he only needs to add a compilation option to automatically statically compile the programs in the library.
As for the Application library I am talking about, the URL is as follows, which is a nix library.
Thank you for your time, support, and willingness to assist a newcomer like me.

1 Like

Ahh ok that clears things up, I never heard the term library in this context.

Alright so that is a little misleading, but yes, you can get the end result you want. Let’s go over the basics. I’ll use flake-enabled nix for this because I think it’s a little easier to understand.

I assume you have Nix already installed and that you are on a x86-64 linux machine. If not, tell me, and I’ll help you navigate through cross-compilation as well.

Enabling flakes

First, create a file ~/.config/nix/nix.conf with the following content:

extra-experimental-features = flakes nix-command

Confirm this works by running this command:

$ nix flake metadata nixpkgs
Resolved URL:  github:NixOS/nixpkgs/842e90934a352f517d23963df3ec0474612e483c
Locked URL:    github:NixOS/nixpkgs/842e90934a352f517d23963df3ec0474612e483c
Description:   A collection of packages for the Nix package manager
Path:          /nix/store/426grpcknijk419cffsznb8bs28d7l3w-source
Revision:      842e90934a352f517d23963df3ec0474612e483c
Last modified: 2023-07-08 08:42:17

Build a simple nix package

You can “build” a package from nixpkgs like so:

nix build nixpkgs#hello

This will check if the package is available on, and just download it instead of building it.

It will also put a result symlink in your current directory, so you can inspect the output:

$ readlink result  
$ file result/bin/hello
result/bin/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/ld03l52xq2ssn4x0g5asypsxqls40497-glibc-2.37-8/lib/, for GNU/Linux 3.10.0, not stripped

You can check out the documentation for nix build (also locally available with man nix3-build).

To build another package, just change hello above to the name of the package.

Building a static package

Just insert pkgsStatic. in front of the package name.

$ nix build nixpkgs#pkgsStatic.hello
$ file result/bin/hello
result/bin/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

That’s it.

Additional info

It’s not guaranteed that all packages in nixpkgs will build this way. This uses musl, which some packages are incompatible with, and CI doesn’t test for this AFIAK, so you may encounter compilation issues.

If you yourself are doing research, you’re probably interested in your results being reproducible. In this case, referring to nixpkgs directly in your command is not a good idea, as the version of nixpkgs this refers to will update periodically. To always refer to the same version, you can either always refer to a specific commit like this:

nix build github:NixOS/nixpkgs/f178210#pkgsStatic.hello

Or clone the nixpkgs repository with git clone --depth=1, cd into it, and then run:

nix build .#pkgsStatic.hello

Hope this helps :slight_smile:


Yes, that’s what I wanted to accomplish. I would like to take a moment to express my deepest appreciation for the significant help and support you have given me. Your guidance in compiling the Nixpkgs library application using static Nix has meant a great deal to me.

Your help is sincerely appreciated. I am truly blessed to have been guided by someone as knowledgeable and experienced as you.

If I ever have the opportunity to return the favor, or if you need any help in the future, please feel free to contact me. I would love to give back to the community that has helped me so much.

I wish you all the best and all the best!