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

60 lines
2.6 KiB
Markdown
Raw Normal View History

2023-07-04 04:18:46 +00:00
# Running Downloaded Binaries on NixOS
2023-06-23 14:22:13 +00:00
2023-07-04 04:18:46 +00: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 14:22:13 +00:00
2023-07-06 03:04:52 +00: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 14:22:13 +00:00
2023-07-04 04:18:46 +00: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 14:22:13 +00:00
```nix
{ config, pkgs, lib, ... }:
{
# ......omit many configurations
environment.systemPackages = with pkgs; [
# ......omit many packages
2023-07-04 04:18:46 +00:00
# Create an FHS environment using the command `fhs`, enabling the execution of non-NixOS packages in NixOS!
2023-07-03 01:46:09 +00:00
(let base = pkgs.appimageTools.defaultFhsEnvArgs; in
pkgs.buildFHSUserEnv (base // {
2023-06-23 14:22:13 +00:00
name = "fhs";
targetPkgs = pkgs: (
2023-07-04 04:18:46 +00: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 14:22:13 +00:00
#
2023-07-04 04:18:46 +00:00
# pkgs.appimageTools provides basic packages required by most software.
2023-07-03 01:46:09 +00:00
(base.targetPkgs pkgs) ++ with pkgs; [
2023-06-23 14:22:13 +00:00
pkg-config
ncurses
2023-07-04 04:18:46 +00:00
# Feel free to add more packages here if needed.
2023-06-23 14:22:13 +00:00
]
);
profile = "export FHS=1";
runScript = "bash";
extraOutputsToInstall = ["dev"];
}))
];
# ......omit many configurations
}
```
2023-07-04 04:18:46 +00: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 14:22:13 +00:00
```shell
2023-07-04 04:18:46 +00:00
# Activating FHS drops me into a shell that resembles a "normal" Linux environment.
2023-06-23 14:22:13 +00:00
$ fhs
2023-07-04 04:18:46 +00:00
# Check what we have in /usr/bin.
2023-06-23 14:22:13 +00:00
(fhs) $ ls /usr/bin
2023-07-04 04:18:46 +00:00
# Try running a non-NixOS binary downloaded from the Internet.
2023-06-23 14:22:13 +00:00
(fhs) $ ./bin/code
```
## References
2023-07-04 04:18:46 +00: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 11:41:40 +00:00
- [nix-alien](https://github.com/thiagokokada/nix-alien): Run unpatched binaries on Nix/NixOS
2023-06-23 14:41:34 +00:00
2023-07-04 04:18:46 +00:00
[Tips&Tricks for NixOS Desktop - NixOS Discourse]: https://discourse.nixos.org/t/tips-tricks-for-nixos-desktop/28488