nix module.nix

pulse.nix
{ pulseaudioFull }:

pulseaudioFull.overrideDerivation (old: {
    src = /home/clever/x/pulseaudio-7.1-modified;
    enableParallelBuilding = true;
})
module.nix
{ pkgs, ... }:

{
  services.udev.extraRules = ''
    SUBSYSTEM!="sound", GOTO="pulseaudio_end"
    ACTION!="change", GOTO="pulseaudio_end"
    KERNEL!="card*", GOTO="pulseaudio_end"

    SUBSYSTEMS=="pci", RESULT=="?*", ATTRS{vendor}=="0x1002", ATTRS{device}=="0x4383", ENV{PULSE_PROFILE_SET}="formula-z.conf"

    LABEL="pulseaudio_end"
  '';
  hardware = {
    pulseaudio = {
      enable = true;
      package = pkgs.pulseCustom;
      configFile = pkgs.runCommand "config.pa" {} ''
        cat ${./default.pa} ${./extra.pa} > $out
      '';
    };
  };
  nixpkgs.config = {
    packageOverrides = pkgs: rec {
      pulseCustom = pkgs.callPackage ./pulse.nix { };
    };
  };
}

nix libobjc2.nix

libobjc2.nix
{ stdenv, cmake, pkgconfig, libbsd, nasm, udev, fetchFromGitHub, clang, gnustepbase }:

stdenv.mkDerivation {
  name = "libobjc2";
  src = fetchFromGitHub {
    owner = "gnustep";
    repo = "libobjc2";
    rev = "2ea11117a392646d960f1e861494e2f27873b15b";
    sha256 = "0blbl4k94ll10qq1c0p35dqgr5029ap9kca5h6zx5ws1pg6p35v1";
  };
  buildInputs = [ cmake clang ];
  NIX_CFLAGS_COMPILE = [ "-I${stdenv.cc.cc}/include/c++/4.9.3/" "-I${stdenv.cc.cc}/include/c++/4.9.3/x86_64-unknown-linux-gnu/" ];
  cmakeFlags = [ "-DCMAKE_CXX_COMPILER=clang" "-DCMAKE_C_COMPILER=clang" ];
}

nix core.nix

overrides.nix
{ 
  packageOverrides = pkgs: rec {
    openiscsi = pkgs.callPackage ./open-iscsi.nix {};
    toxvpn = pkgs.callPackage ./toxvpn.nix {};
    util = pkgs.callPackage ./util.nix {};
    chromefix = pkgs.callPackage ./chrome-fix.nix {};
    #nix = pkgs.stdenv.lib.overrideDerivation pkgs.nix (oldAttrs: {
    #  patches = [ ./status.patch ];
    #});
    linux_rpi_41 = let
      rev = "9e8e5dc45d7d4cafdfd38e0e809f873e78bbc8e9";
    in
    import <nixpkgs/pkgs/os-specific/linux/kernel/generic.nix> ( rec {
      inherit (pkgs) stdenv perl buildLinux;
      version = "4.1.10-${rev}";
      modDirVersion = "4.1.10";
      src = pkgs.fetchurl {
        url = "https://api.github.com/repos/raspberrypi/linux/tarball/${rev}";
        name = "linux-raspberrypi-${version}.tar.gz";
        sha256 = "1kjr15x54xs06ip7z3kcvbiylcs8pcp4dccd4jwywiyrnyl0i0qv";
      };
    });
    linuxPackages_rpi41 = pkgs.linuxPackagesFor linux_rpi_41 pkgs.linuxPackages_rpi;
    raspberrypifw = pkgs.callPackage ./raspberrypifw.nix {};
    rpPPPoE = pkgs.stdenv.lib.overrideDerivation pkgs.rpPPPoE (oldAttrs: {
      postInstall = ''
        echo noauth > $out/etc/ppp/pppoe-server-options
        echo lcp-echo-interval 10 >> $out/etc/ppp/pppoe-server-options
        echo lcp-echo-failure 2 >> $out/etc/ppp/pppoe-server-options
      '';
    });
  };
}
core.nix
  nixpkgs.config = {
    sqlite.interactive = true;
    packageOverrides = (import ./overrides.nix).packageOverrides;
  };

nix foo.nix

foo.nix
with import <nixpkgs> {};

{
  webcore = callPackage ./webcore.nix {};
  webapp1 = callPackage ./webapp1.nix {};
}

nix default.nix

input.c
#include <iostream>

#include "shared.hh"
#include "eval.hh"
#include "common-opts.hh"
#include "eval-inline.hh"

using namespace nix;

typedef set<Value*> ValuesSeen;

int displ = 0;

void addVarToScope(const Symbol &name, Value &v, Env *env, StaticEnv &staticEnv) {
  staticEnv.vars[name] = displ;
  env->values[displ++] = &v;
}

Expr *parseString(string s, EvalState &state, StaticEnv &staticEnv) {
  Expr *e = state.parseExprFromString(s,".",staticEnv);
  return e;
}

void addAttrsToScope(Value &attrs, EvalState &state, Env *env, StaticEnv &staticEnv) {
  state.forceAttrs(attrs);
  for (auto & i : *attrs.attrs) addVarToScope(i.name, *i.value, env, staticEnv);
}

std::ostream &printValue(std::ostream &str, Value &v, unsigned int maxDepth, ValuesSeen &seen, EvalState &state) {
  str.flush();
  state.forceValue(v);
  switch (v.type) {
  case tAttrs: {
    seen.insert(&v);
    bool isDrv = state.isDerivation(v);
    if (isDrv) {
      str << "<<derivation ";
      Bindings::iterator i = v.attrs->find(state.sDrvPath);
      PathSet context;
      Path drvPath = i != v.attrs->end() ? state.coerceToPath(*i->pos, *i->value, context) : "???";
      str << drvPath << ">>";
    }
  }
  }
}

int main(int argc, char **argv) {
  initNix();
  initGC();
  Strings searchPath;
  EvalState state(searchPath);
  StaticEnv staticEnv(false, &state.staticBaseEnv);
  Env *env;
  int envSize = 32768;

  env = &state.allocEnv(envSize);
  env->up = &state.baseEnv;
  
  Value v1,v2;
  state.evalFile(lookupFileArg(state,"<nixpkgs>"),v1);
  Bindings &bindings(*state.allocBindings(0));
  state.autoCallFunction(bindings,v1,v2);
  addAttrsToScope(v2, state, env, staticEnv);

  Value v;
  Expr *e = parseString("firefox", state, staticEnv);
  e->eval(state, *env, v);
  state.forceValue(v);
  ValuesSeen seen;
  printValue(std::cout,v,2,seen,state);
}
default.nix
{ nixpkgs ? <nixpkgs>, system ? builtins.currentSystem }:

with import nixpkgs { inherit system; };

let nix = enableDebugging nixUnstable; in

runCommand "test"
  { buildInputs = [ nix boehmgc ]; dontStrip = true; }
  ''
    mkdir -p $out/bin/
    g++ -g -O3 -Wall -std=c++0x -o $out/bin/test1 ${./input.c} -I${nix}/include/nix -lnixformat -lnixutil -lnixstore -lnixexpr -lnixmain -lgc -DNIX_VERSION=\"${(builtins.parseDrvName nix.name).version}\"
  ''

nix cwrap.nix

torch.nix
with import <nixpkgs> {};

let
  cwrap = pkgs.callPackage ./cwrap.nix {};
in
stdenv.mkDerivation rec {
  name = "torch-7";

  src = fetchFromGitHub {
    owner = "torch";
    repo = "torch7";
    rev = "adfee5da9740aab2ebf647ecffe1102e8e3ccef5";
    sha256 = "0bl2ygfyapvfb2z5w2wk6v0ayg82a5xxh1449shvn7wv1815qs9g";
  };

  cmakeFlags = [ "-DLUA=${pkgs.lua}/bin/lua" ];

  buildInputs = [ cwrap cmake readline qt4 libjpeg libpng ncurses imagemagick gfortran gnuplot lua ];
}
cwrap.nix
{ stdenv, lua, fetchFromGitHub }:

stdenv.mkDerivation rec {
  name = "cwrap-7";

  src = fetchFromGitHub {
    owner = "torch";
    repo = "cwrap";
    rev = "6e7d52f0a359dee47127386adb03c0dc4ddd4766";
    sha256 = "01yh9z0axz0b4f87mbhkzh64dyv6c4jdkazcpmy1bl1am8apbd8w";
  };

  installPhase = ''
    mkdir -p $out/share/lua/5.2/cwrap/ $out/nix-support
    cp -v init.lua types.lua cinterface.lua $out/share/lua/5.2/cwrap/
    cat <<EOF > $out/nix-support/setup-hook
    # FIXME, overrides all other lua packages, doesnt persist past compile
    export LUA_PATH=$out/share/lua/5.2/?/init.lua\;$out/share/lua/5.2/?.lua\;?.lua
    EOF
  '';

  buildInputs = [ lua ];
}

nix LAN1-default.nix

router-default.nix
{ system ? builtins.currentSystem }:

let
  configuration = { config, pkgs, ...}: {
    imports = [
      <nixos/modules/virtualisation/qemu-vm.nix>
      ../nixcfg/router.nix
    ];
    config = {
      virtualisation = {
        graphics = false;
        qemu.networkingOptions = [
          "-net nic,vlan=0,model=virtio" "-net socket,vlan=0,connect=127.0.0.1:5001" "-net dump,vlan=0,file=/home/clever/router/router/1q.cap"
          "-net nic,vlan=1,model=virtio" "-net socket,vlan=1,listen=0.0.0.0:5002"
        ];
      };
      users.extraUsers.root.password = "root";
      networking.hostName = "router";
    };
  };
  eval = import <nixos/lib/eval-config.nix> {
    inherit system;
    modules = [ configuration ];
  };
in
{
  raw = eval;
  vm = eval.config.system.build.vm;
}
modem-default.nix
{ system ? builtins.currentSystem }:

let
  configuration = { config, pkgs, ...}: {
    imports = [ <nixos/modules/virtualisation/qemu-vm.nix> ];
    config = {
      virtualisation = {
        graphics = false;
        qemu.networkingOptions = [
          "-net nic,vlan=0,model=virtio" "-net user,vlan=0"
          "-net nic,vlan=1,model=virtio" "-net socket,vlan=1,listen=0.0.0.0:5001" "-net dump,vlan=1,file=/home/clever/router/modem/1q.cap"
        ];
      };
      services = {
        dhcpd = {
          interfaces = [ "eth1.34" "eth1.35" ];
          enable = true;
          extraConfig = ''
            subnet 10.0.0.0 netmask 255.255.255.0 {
              range 10.0.0.100 10.0.0.200;
            }
            subnet 192.168.100.0 netmask 255.255.255.0 {
              range 192.168.100.100 192.168.100.200;
            }
          '';
        };
      };
      networking = {
        enableIPv6 = false;
        hostName = "modem";
        firewall.enable = false;
        interfaces = {
          eth1.useDHCP = false;
          "eth1.34" = {
            ipAddress = "10.0.0.1";
            prefixLength = 24;
          };
          "eth1.35" = {
            ipAddress = "192.168.100.1";
            prefixLength = 24;
          };
        };
      };
      boot.kernelModules = [ "8021q" ];
      users.extraUsers.root.password = "root";
      environment.systemPackages = with pkgs; [ vlan nmap tcpdump ];
      systemd.services = {
        network-local-commands.path = with pkgs; [ iproute vlan ];
        network-vlans = {
          description = "network vlan-start";
          before = [ "network-pre.target" ];
          wantedBy = [ "network-pre.target" ];
          unitConfig.ConditionCapability = "CAP_NET_ADMIN";
          serviceConfig.Type = "oneshot";
          serviceConfig.RemainAfterExit = true;
          path = [ pkgs.vlan pkgs.iproute ];
          script = ''
            vconfig add eth1 34
            vconfig add eth1 35
            ip link set eth1 up
          '';
        };
      };
    };
  };
  eval = import <nixos/lib/eval-config.nix> {
    inherit system;
    modules = [ configuration ];
  };
in
{ 
  raw = eval;
  vm = eval.config.system.build.vm;
}
lan1-default.nix
{ system ? builtins.currentSystem }:

let
  configuration = { config, pkgs, ...}: {
    imports = [
      <nixos/modules/virtualisation/qemu-vm.nix>
      ./configuration.nix
    ];
    config = {
      virtualisation = {
        graphics = false;
        qemu.networkingOptions = [
          "-net nic,vlan=0,model=virtio" "-net socket,vlan=0,connect=127.0.0.1:5002"
        ];
      };
      users.extraUsers.root.password = "root";
      networking.hostName = "lan1";
    };
  };
  eval = import <nixos/lib/eval-config.nix> {
    inherit system;
    modules = [ configuration ];
  };
in
{
  raw = eval;
  vm = eval.config.system.build.vm;
}

nix configuration.nix

configuration.nix
{ lib, config, pkgs, ... }:

{
  imports = [ ./iscsi-boot.nix ];
  fileSystems = {
    "/" = { device = "UUID=132e7c5b-b4a9-4154-8105-4479e17f4f5b"; fsType = "ext4"; };
    "/boot/" = { device = "UUID=fdac080d-d111-455c-a890-bc3e5e08c2d5"; fsType = "ext4"; };
  };
  boot = {
    loader = {
      grub.enable = true;
    };
    initrd = {
      kernelModules = [ "e1000e" ];
      iscsi = {
        initiatorName = "iqn.2015-09.com.example:3255a7223b2";
        devices = [
          { host = "192.168.2.61"; lun = "iqn.2001-04.com.c2d-nix5"; }
        ];
        ipAddress = "192.168.2.31";
        netDev = "eth0";
        prefixLength = 24;
        defaultGateway = "192.168.2.1";
      };
    };
  };
  networking.interfaces.eth0.ipAddress = "192.168.2.31";
  networking.interfaces.eth0.prefixLength = 24;
  networking.defaultGateway = "192.168.2.1";
  networking.hostName = "iscsi-boot";
  networking.nameservers = [ "192.168.2.61" ];
  networking.dhcpcd.persistent = true;
  environment.systemPackages = [ ];
}
instructions
# find the targets available on a host
iscsi_discovery 192.168.2.61
# connect to one
iscsiadm -m node -T iqn.2001-04.com.c2d-nix4 -p 192.168.2.61 -l
# in my case, it appears as sdd, check dmesg
[root@nixos:~]# hexdump -C /dev/sdd
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
# format and mount to /mnt like a normal nixos install
mkfs.ext4 /dev/sdd
mount -v /dev/sdd /mnt/
# cheat to aid in qemu testing
mount --bind /home/clever/iscsi-boot/boot/ /mnt/boot/
# paste the following into /mnt/etc/nixos/configuration.nix
{ lib, config, pkgs, ... }:

{
  imports = [ ./iscsi-boot.nix ];
  fileSystems."/" = { device = "UUID=35d3ccb9-89d8-4f3a-a6da-1d94d4d94f8a"; fsType = "ext4"; };
  boot = {
    loader = {
      grub.enable = false;
      generic-extlinux-compatible.enable = true;
    };
    initrd = {
      kernelModules = [ "e1000" ];
      iscsi.initiatorName = "iqn.2015-09.com.example:3255a7223b2";
      iscsi.devices = [
        { host = "192.168.2.61"; lun = "iqn.2001-04.com.c2d-nix4"; }
      ];
    };
  };
}
# and then install like usual
nixos-install
# un-mount everything
umount /mnt/boot /mnt
# launch the system under qemu
/nix/store/c22c7vrjxpji16bbg24rs1rg6ngx3y38-qemu-x86-only-2.4.0/bin/qemu-system-x86_64 -enable-kvm -m 1024 -kernel boot/nixos/q50nnkhsmfbqmbn2imnflfiqhj1hp6x0-linux-3.18.21-bzImage -initrd boot/nixos/kcaqy563wwr5f7cxlscsp0j2qfhnz92z-initrd-initrd -append console=ttyS0 panic=-1 boot.shell_on_fail init=/nix/store/idpgwjsiggc5k33p2il9kr5s3np92wgp-nixos-system-nixos-16.03pre69426.2a574cb/init -no-reboot -net nic,vlan=0,model=e1000 -net user,vlan=0 -net dump,vlan=0 -serial stdio
iscsi-boot.nix
{ config, lib, pkgs, ... }:

with lib;

let
  open-iscsi = pkgs.callPackage ./open-iscsi.nix {};
  openCommand = { host, lun, ... }: ''
    iscsistart -t ${lun} -a ${host} -i ${config.boot.initrd.iscsi.initiatorName} -g 0
  '';
in
{
  options = {
    boot.initrd.iscsi.initiatorName = mkOption {
      default = "";
      example = "iqn.2015-09.com.example:3255a7223b2";
      type = types.string;
      description = "the initiator name used when connecting";
    };
    boot.initrd.iscsi.devices = mkOption {
      default = [ ];
      type = types.listOf types.optionSet;
      options = {
        host = mkOption {
          example = "192.168.2.61";
          type = types.string;
          description = "the iscsi target";
        };
        lun = mkOption {
          example = "iqn.2015-01.com.example:san.img";
          type = types.string;
          description = "the LUN to connect";
        };
      };
    };
  };
  config = mkIf (config.boot.initrd.iscsi.devices != []) {
    boot.initrd.kernelModules = [ "iscsi_tcp" ];
    boot.initrd.availableKernelModules = [ "crc32c" ];
    boot.initrd.preLVMCommands = ''
      export PATH=$PATH:${open-iscsi.iscsistart}/bin/
      mkdir -pv /var/run/ /var/lock/
      ip link set ${cfg.netDev} up
      ip addr add ${cfg.ipAddress}/${toString cfg.prefixLength} dev ${cfg.netDev}
      ip route add via ${cfg.defaultGateway} dev ${cfg.netDev}
      sleep 20
    '' + concatMapStrings openCommand config.boot.initrd.iscsi.devices + ''
      ls -lR /dev/disk/
    '';
  };
}
open-iscsi.nix
{ stdenv, fetchurl }:
let
  pname = "open-iscsi-2.0-873";
in stdenv.mkDerivation {
  name = "${pname}";
  outputs = [ "out" "iscsistart" ];
  
  src = fetchurl {
    url = "http://www.open-iscsi.org/bits/${pname}.tar.gz";
    sha256 = "1nbwmj48xzy45h52917jbvyqpsfg9zm49nm8941mc5x4gpwz5nbx";
  };
  
  DESTDIR = "$(out)";
  
  preConfigure = ''
    sed -i 's|/usr/|/|' Makefile
  '';
  # TODO, its staticaly linked, but refers to glibc locale files, dependancies need a trim
  postInstall = ''
    mkdir -pv $iscsistart/bin/
    cp -v usr/iscsistart $iscsistart/bin/
  '';
  
  meta = {
    description = "A high performance, transport independent, multi-platform implementation of RFC3720";
    license = stdenv.lib.licenses.gpl2Plus;
    homepage = http://www.open-iscsi.org;
  };
}

nix 修复 - themes.php,找不到plugins.php错误

修复 - themes.php,找不到plugins.php错误

new_gist_file.nix
# reference: https://wordpress.org/support/topic/not-found-update-corephp-themesphp-and-pluginsphp
# on htaccess add the following line

AddHandler application/x-httpd-php5 .php



# Other uses:
# Reference:
# http://kb.siteground.com/how_to_have_different_php__mysql_versions/

To switch to PHP 4.4:

AddHandler application/x-httpd-php4 .php .php4 .php3

To switch to PHP 5.0:

AddHandler application/x-httpd-php5 .php .php5 .php4 .php3

To switch to PHP 5.1:

AddHandler application/x-httpd-php51 .php .php5 .php4 .php3

To switch to PHP 5.2:

AddHandler application/x-httpd-php52 .php .php5 .php4 .php3

To switch to PHP 5.3:

AddHandler application/x-httpd-php53 .php .php5 .php4 .php3

To switch to PHP 5.4:

AddHandler application/x-httpd-php54 .php .php5 .php4 .php3

To switch to PHP 5.5:

AddHandler application/x-httpd-php55 .php .php5 .php4 .php3

To switch to PHP 5.6:

AddHandler application/x-httpd-php56 .php .php5 .php4 .php3

To switch to the secure PHP 5.2 with Suhosin patch:

AddHandler application/x-httpd-php52s .php .php5 .php4 .php3

nix htaccess拒绝使用IP访问

htaccess拒绝使用IP访问

htaccesdeny_gist_file.nix
#deny access from files in the directory from everybody:

<Limit GET POST PUT>
     order deny, all
     deny from all
</Limit

# deny access to everybody but myself
<Limit GET POST PUT>
     order deny, all
     deny from all
     allow from 127.0.0.1
     # use allow from IP  for any other IPs
     # use $_SERVER['SERVER_ADDR'];  in php to output your IP
</Limit