Jump to content

OpenWRT: Difference between revisions

From NixOS Wiki
imported>SebTM
m Duplicate headline
Dass (talk | contribs)
Add dewclaw for declarative configuration
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
is an open-source project for embedded operating systems based on Linux, primarily used on embedded devices to route network traffic. The main components are Linux, util-linux, musl, and BusyBox. All components have been optimized to be small enough to fit into the limited storage and memory available in home routers. (Source: https://en.wikipedia.org/wiki/OpenWrt)
is an open-source project for embedded operating systems based on Linux, primarily used on embedded devices to route network traffic. The main components are Linux, util-linux, musl, and BusyBox. All components have been optimized to be small enough to fit into the limited storage and memory available in home routers. (Source: https://en.wikipedia.org/wiki/OpenWrt)


OpenWRT provides different possibilities (https://openwrt.org/docs/guide-developer/imagebuilder_frontends) to build custom images for your devices. There is also a little helper tool suggested on IRC called "autobuild" (https://johannes.truschnigg.info/code/openwrt_autobuild/) which helps to manage/keep track of image-configurations for your devices. [TODO mention dependencies]
== ImageBuilder ==


== Using ImageBuilder in nix-shell ==
OpenWRT provides different possibilities (https://openwrt.org/docs/guide-developer/imagebuilder_frontends) to build custom images for your devices. There is also a little helper tool suggested on IRC called "autobuild" (https://johannes.truschnigg.info/code/openwrt_autobuild/) which helps to manage/keep track of image-configurations for your devices. It's also working with the nix-shell below - '''please note''' that there is currently some misbehavior when "make_clean" is set to "True" the system says build is triggered but there are no files/logs/processes running then.
<syntaxHighlight lang=nix>
{ pkgs ? import <nixpkgs> {} }:


let
=== Using ImageBuilder in nix-shell ===
  fixWrapper = pkgs.runCommand "fix-wrapper" {} ''
Get a copy of: https://github.com/nix-community/nix-environments/blob/master/envs/openwrt/shell.nix
    mkdir -p $out/bin
    for i in ${pkgs.gcc.cc}/bin/*-gnu-gcc*; do
      ln -s ${pkgs.gcc}/bin/gcc $out/bin/$(basename "$i")
    done
    for i in ${pkgs.gcc.cc}/bin/*-gnu-{g++,c++}*; do
      ln -s ${pkgs.gcc}/bin/g++ $out/bin/$(basename "$i")
    done
  '';


  fhs = pkgs.buildFHSUserEnv {
{{warning|Currently (2023-04-17), the openwrt build fails with a "/usr/lib/libuuid.so: file not recognized: file format not recognized".}}
    name = "openwrt-env";
    targetPkgs = pkgs: with pkgs;
      [ git
        perl
        gnumake
        gcc
        unzip
        utillinux
        python3
        patch
        wget
        file
        subversion
        which
        pkgconfig
        openssl
        fixWrapper
        systemd
        binutils
 
        ncurses
        zlib
        zlib.static
        glibc.static
      ];
    multiPkgs = null;
    extraOutputsToInstall = [ "dev" ];
    profile = ''
      export hardeningDisable=all
    '';
  };
in fhs.env
</syntaxHighlight>
Source: https://gist.github.com/Mic92/b59054188c595e5652cacf50485583e0 (Updated python to python3 because build of image failed without)


Currently (01/2022) I've tested builds using nixpkgs-21.11 and unstable, and both worked. This could possibly break/behave different in the future so you can e.g. pin to an older nixpkgs-revision by replacing the first lines of the above code with this:
Currently (01/2022) I've tested builds using nixpkgs-21.11 and unstable, and both worked. This could possibly break/behave different in the future so you can e.g. pin to an older nixpkgs-revision by replacing the first lines of the above code with this:
Line 63: Line 19:
     owner = "NixOS";
     owner = "NixOS";
     repo = "nixpkgs";             
     repo = "nixpkgs";             
     rev = "bee172501de3b4496ff81cc1621d307f167e9382";       
     rev = "<revision>";       
     sha256 = "<revision>";                                                   
     sha256 = "<hash>";                                                   
   }) {};
   }) {};
...
...
</syntaxHighlight>
</syntaxHighlight>
=== Using ImageBuilder in a Nix Derivation ===
See https://github.com/astro/nix-openwrt-imagebuilder/
== OpenWRT buildroot ==
It is possible to build OpenWRT from source with buildroot under NixOS too.
You can use the following dev shell with the official OpenWRT build guide:
* https://openwrt.org/docs/guide-developer/toolchain/use-buildsystem
Make sure you disable the nixpkgs compiler hardening, as the OpenWRT toolchain sources aren't as clean as required by nixpkgs defaults.
<syntaxHighlight lang=nix>
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  };
  outputs = { self, nixpkgs, ... }: {
    devShells."x86_64-linux".default = let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
      };
    in pkgs.mkShell {
      nativeBuildInputs = with pkgs; [
        git
        pkg-config
        ncurses
        unzip
        python3
        cdrtools
      ];
      hardeningDisable = [ "all" ];
    };
  };
}
</syntaxHighlight>
== Declarative Configuration ==
Dewclaw, a semi-declarative OpenWrt configuration tool, allows building configs for OpenWRT ([https://discourse.nixos.org/t/dewclaw-semi-declarative-openwrt-configurations/33993 announcement]).  It works by mapping UCI configuration options to Nix. The original project has been picked up and now can be found here: https://github.com/MakiseKurisu/dewclaw.

Latest revision as of 12:08, 18 March 2025

is an open-source project for embedded operating systems based on Linux, primarily used on embedded devices to route network traffic. The main components are Linux, util-linux, musl, and BusyBox. All components have been optimized to be small enough to fit into the limited storage and memory available in home routers. (Source: https://en.wikipedia.org/wiki/OpenWrt)

ImageBuilder

OpenWRT provides different possibilities (https://openwrt.org/docs/guide-developer/imagebuilder_frontends) to build custom images for your devices. There is also a little helper tool suggested on IRC called "autobuild" (https://johannes.truschnigg.info/code/openwrt_autobuild/) which helps to manage/keep track of image-configurations for your devices. It's also working with the nix-shell below - please note that there is currently some misbehavior when "make_clean" is set to "True" the system says build is triggered but there are no files/logs/processes running then.

Using ImageBuilder in nix-shell

Get a copy of: https://github.com/nix-community/nix-environments/blob/master/envs/openwrt/shell.nix

Warning: Currently (2023-04-17), the openwrt build fails with a "/usr/lib/libuuid.so: file not recognized: file format not recognized".

Currently (01/2022) I've tested builds using nixpkgs-21.11 and unstable, and both worked. This could possibly break/behave different in the future so you can e.g. pin to an older nixpkgs-revision by replacing the first lines of the above code with this:

{ opkgs ? import <nixpkgs> {} }:                           
                                                           
let                                                        
  pkgs = import (opkgs.fetchFromGitHub {                   
    owner = "NixOS";
    repo = "nixpkgs";            
    rev = "<revision>";      
    sha256 = "<hash>";                                                   
  }) {};
...

Using ImageBuilder in a Nix Derivation

See https://github.com/astro/nix-openwrt-imagebuilder/


OpenWRT buildroot

It is possible to build OpenWRT from source with buildroot under NixOS too.

You can use the following dev shell with the official OpenWRT build guide:

Make sure you disable the nixpkgs compiler hardening, as the OpenWRT toolchain sources aren't as clean as required by nixpkgs defaults.

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  };
  outputs = { self, nixpkgs, ... }: {
    devShells."x86_64-linux".default = let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
      };
    in pkgs.mkShell {
      nativeBuildInputs = with pkgs; [
        git
        pkg-config
        ncurses
        unzip
        python3
        cdrtools
      ];
      hardeningDisable = [ "all" ];
    };
  };
}

Declarative Configuration

Dewclaw, a semi-declarative OpenWrt configuration tool, allows building configs for OpenWRT (announcement). It works by mapping UCI configuration options to Nix. The original project has been picked up and now can be found here: https://github.com/MakiseKurisu/dewclaw.