nixos-and-flakes-book/docs/best-practices/run-downloaded-binaries-on-nixos.md

61 lines
2.6 KiB
Markdown
Raw Normal View History

2023-07-04 06:18:46 +02:00
# Running Downloaded Binaries on NixOS
2023-06-23 16:22:13 +02:00
2023-07-04 06:18:46 +02:00
Since NixOS does not strictly adhere to the Filesystem Hierarchy Standard (FHS), binaries downloaded from the internet may not work directly on NixOS. However, there are various methods available to make them function properly.
2023-06-23 16:22:13 +02:00
2023-07-06 05:04:52 +02:00
For a comprehensive guide that presents ten different approaches to run downloaded binaries on NixOS, I recommend reading the article [Different methods to run a non-nixos executable on Nixos](https://unix.stackexchange.com/questions/522822/different-methods-to-run-a-non-nixos-executable-on-nixos) and take a look at [nix-alien](https://github.com/thiagokokada/nix-alien).
2023-06-23 16:22:13 +02:00
2023-07-04 06:18:46 +02:00
Among these methods, I personally prefer creating an FHS environment to run the binary, as it proves to be both convenient and easy to use. To set up such an environment, you can add the following code to one of your Nix modules:
2023-06-23 16:22:13 +02:00
```nix
{ config, pkgs, lib, ... }:
{
# ......omit many configurations
environment.systemPackages = with pkgs; [
# ......omit many packages
2023-07-04 06:18:46 +02:00
# Create an FHS environment using the command `fhs`, enabling the execution of non-NixOS packages in NixOS!
2023-07-08 12:31:45 +02:00
(let base = pkgs.appimageTools.defaultFhsEnvArgs; in
2023-07-03 03:46:09 +02:00
pkgs.buildFHSUserEnv (base // {
2023-06-23 16:22:13 +02:00
name = "fhs";
targetPkgs = pkgs: (
2023-07-04 06:18:46 +02:00
# pkgs.buildFHSUserEnv provides only a minimal FHS environment,
# lacking many basic packages needed by most software.
# Therefore, we need to add them manually.
2023-06-23 16:22:13 +02:00
#
2023-07-04 06:18:46 +02:00
# pkgs.appimageTools provides basic packages required by most software.
2023-07-03 03:46:09 +02:00
(base.targetPkgs pkgs) ++ with pkgs; [
2023-06-23 16:22:13 +02:00
pkg-config
ncurses
2023-07-04 06:18:46 +02:00
# Feel free to add more packages here if needed.
2023-06-23 16:22:13 +02:00
]
);
profile = "export FHS=1";
runScript = "bash";
extraOutputsToInstall = ["dev"];
}))
];
# ......omit many configurations
}
```
2023-07-04 06:18:46 +02:00
After applying the updated configuration, you can use the `fhs` command to enter the FHS environment, and then execute the binary you downloaded, for example:
2023-06-23 16:22:13 +02:00
```shell
2023-07-04 06:18:46 +02:00
# Activating FHS drops me into a shell that resembles a "normal" Linux environment.
2023-06-23 16:22:13 +02:00
$ fhs
2023-07-04 06:18:46 +02:00
# Check what we have in /usr/bin.
2023-06-23 16:22:13 +02:00
(fhs) $ ls /usr/bin
2023-07-04 06:18:46 +02:00
# Try running a non-NixOS binary downloaded from the Internet.
2023-06-23 16:22:13 +02:00
(fhs) $ ./bin/code
```
## References
2023-07-04 06:18:46 +02:00
- [Tips&Tricks for NixOS Desktop - NixOS Discourse][Tips&Tricks for NixOS Desktop - NixOS Discourse]: This resource provides a collection of useful tips and tricks for NixOS desktop users.
2023-07-05 13:41:40 +02:00
- [nix-alien](https://github.com/thiagokokada/nix-alien): Run unpatched binaries on Nix/NixOS
2023-06-23 16:41:34 +02:00
2023-07-08 12:31:45 +02:00
[Tips&Tricks for NixOS Desktop - NixOS Discourse]: https://discourse.nixos.org/t/tips-tricks-for-nixos-desktop/28488