Help with replacing package with self compiled version

Hello

I have folowing parts in my /etc/nixos/configuration.nix:

systemd.services.hostapd.wantedBy= [ "multi-user.target" ];
  systemd.services.hostapd.after = [ "sys-subsystem-net-devices-wlp4s0.device" "nat.service" "bind>

  services.hostapd = {

Unfortunatelly this hostapd version seems not to support wifi 6, hence I complied my own version with “CONFIG_IEEE80211AX=y”.
For that I have created in /root a folder structure:

/root/nixos/private-packages
├── default.nix
└── pkgs
    └── hostapd
        └── default.nix

/root/nixos/private-packages/default.nix:

{ system ? builtins.currentSystem }:                                                   
                                                                                   
let                                                                                    
   pkgs = import <nixpkgs> { inherit system; };                                         
                                                                                   
   callPackage = pkgs.lib.callPackageWith (pkgs // self);                               
                                                                                   
   self = {                                                                             
      hostapd = callPackage ./pkgs/hostapd { };                                            
   };                                                                                   
   in self

/root/nixos/private-packages/pkgs/hostapd/default.nix:

{
lib
, stdenv
, fetchurl
, pkg-config
, libnl
, openssl 
}:

stdenv.mkDerivation {
  name = "hostapd";

  src = fetchurl {
    url = "https://w1.fi/cgit/hostap/snapshot/hostap_2_10.tar.gz";
    sha256 = "f39df26eb76757d8ab3a27178545e2ead33ce32daddef771cd38c35893e8c392";
  };
    nativeBuildInputs = [ pkg-config ];
  buildInputs = [ libnl openssl ];

  extraConfig = ''
    CONFIG_DRIVER_WIRED=y
    CONFIG_LIBNL32=y
    CONFIG_EAP_SIM=y
    CONFIG_EAP_AKA=y
    CONFIG_EAP_AKA_PRIME=y
    CONFIG_EAP_PAX=y
    CONFIG_EAP_PWD=y
    CONFIG_EAP_SAKE=y
    CONFIG_EAP_GPSK=y
    CONFIG_EAP_GPSK_SHA256=y
    CONFIG_EAP_FAST=y
    CONFIG_EAP_IKEV2=y
    CONFIG_EAP_TNC=y
    CONFIG_EAP_EKE=y
    CONFIG_RADIUS_SERVER=y
    CONFIG_IEEE80211R=y
    CONFIG_IEEE80211N=y
    CONFIG_IEEE80211AC=y
    CONFIG_IEEE80211AX=y
    CONFIG_FULL_DYNAMIC_VLAN=y
    CONFIG_VLAN_NETLINK=y
    CONFIG_TLS=openssl
    CONFIG_TLSV11=y
    CONFIG_TLSV12=y
    CONFIG_INTERNETWORKING=y
    CONFIG_HS20=y
    CONFIG_ACS=y
    CONFIG_GETRANDOM=y
    CONFIG_SAE=y
  '';

  postPatch = ''
    substituteInPlace hostapd/Makefile --replace /usr/local $out
  '';

  configurePhase = ''
    runHook preConfigure
    cd hostapd
    cp -v defconfig .config
    echo "$extraConfig" >> .config
    export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE $(pkg-config --cflags libnl-${lib.versions.major libnl.version}.0)"
    runHook postConfigure
  '';

  preInstall = ''
    mkdir -p $out/bin
  '';

  meta = with lib; {
    homepage = "https://w1.fi/cgit/hostap/";
    description = "A  wifi access point tool";
    license = licenses.bsd3;
    maintainers = with maintainers; [ Jouni Malinen ];
    platforms = platforms.linux;
  };
}

I can compile and install the package with:

export NIX_PATH=custompkgs=/root/nixos/private-packages/default.nix:$NIX_PATH
nix-env -f '<custompkgs>' -iA hostapd

But how could I integrate it in /etc/nixos/configuration.nix that the default hostapd service will be overwritten with the self compiled version. I’m new to nixos, any help would be highly appriciated.

Regards Sven

Hello,

What you are probably looking for is the overlay mechanism.

Here is the documentation in the nixpkgs manual and here is a better (imo) documentation in the wiki.

Curiously, there is little mention of overlays in the nixos documentation, I understand that is not strictly speaking a nixos mechanism but as a way to change packages in your system, we might want to link to the corresponding nixpkgs chapter.

Thx a lot for quick respond. It is a lot of information, so it’s still difficult for a newbe, but I think I got it:
I added to my /etc/nixos/configuration.nix

  nixpkgs.overlays = [ (self: super:
  {
    hostapd = super.callPackage /root/nixos/private-packages/pkgs/hostapd/default.nix {};
  }
  ) ];

It is for sure not the most elegant solution, but it seems to work for now.

Regards Sven