OpenWRT: Difference between revisions

From NixOS Wiki
imported>Jtojnar
avoid deprecated github:NixOS/nixpkgs-channels
imported>SebTM
Enhance/extend documentation for backlink from OpenWRT-Wiki
Line 1: Line 1:
{{Expansion}}
== 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)


== Building OpenWRT with a 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. [TODO mention dependencies]
Download [https://gist.github.com/Mic92/b59054188c595e5652cacf50485583e0 this Gist] as your <code>shell.nix</code> in the same folder as openwrt and run <code>nix-shell</code> in this folder.
 
This gist has been tested with the following inputs:
== Using ImageBuilder in nix-shell ==
* Nixpkgs Revision <code>bee172501de3b4496ff81cc1621d307f167e9382</code>
<syntaxHighlight lang=nix>
* OpenWRT revision <code>0f063f8ac70943ff1f383c9bd05d1792730fe614</code>
{ pkgs ? import <nixpkgs> {} }:
 
let
  fixWrapper = pkgs.runCommand "fix-wrapper" {} ''
    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 {
    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:


Due to the very delicate OpenWRT dev environment it may be necessary to pin the nixpkgs revision you are using. This can be done by replacing the first lines of the gist with:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{ opkgs ? import <nixpkgs> {} }:                           
{ opkgs ? import <nixpkgs> {} }:                           
Line 16: Line 65:
     repo = "nixpkgs";             
     repo = "nixpkgs";             
     rev = "bee172501de3b4496ff81cc1621d307f167e9382";       
     rev = "bee172501de3b4496ff81cc1621d307f167e9382";       
     sha256 = "14jn7jiq4likrm7fry2m3q3rmv3y4xjfnwx13wh6iqd8c3bcjd12";                                                   
     sha256 = "<revision>";                                                   
   }) {};
   }) {};
...
...
</syntaxHighlight>
</syntaxHighlight>

Revision as of 22:52, 27 January 2022

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]

Using ImageBuilder in nix-shell

{ pkgs ? import <nixpkgs> {} }:

let
  fixWrapper = pkgs.runCommand "fix-wrapper" {} ''
    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 {
    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

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:

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