fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . autorandr ; 8 9matrixOf = n :m :elemType : 10mkOptionType rec { 11name = "matrixOf" ; 12description = 13" ${ toString n } × ${ toString m } matrix of ${ elemType . description } s" ; 14check = xss : 15let listOfSize = l :xs :isList xs && length xs == l ; 16in listOfSize n xss 17&& all ( xs :listOfSize m xs && all elemType . check xs ) xss ; 18merge = mergeOneOption ; 19getSubOptions = prefix :elemType . getSubOptions ( prefix ++ [ "*" "*" ]); 20getSubModules = elemType . getSubModules ; 21substSubModules = mod :matrixOf n m ( elemType . substSubModules mod ); 22functor = ( defaultFunctor name ) // { wrapped = elemType ; }; 23}; 24 25profileModule = types . submodule { 26options = { 27fingerprint = mkOption { 28type = types . attrsOf types . str ; 29description = '' 30Output name to EDID mapping. 31Use `autorandr --fingerprint` to get current setup values. 32'' ; 33default = { }; 34}; 35 36config = mkOption { 37type = types . attrsOf configModule ; 38description = "Per output profile configuration." ; 39default = { }; 40}; 41 42hooks = mkOption { 43type = profileHooksModule ; 44description = "Profile hook scripts." ; 45default = { }; 46}; 47}; 48}; 49 50configModule = types . submodule { 51options = { 52enable = mkOption { 53type = types . bool ; 54description = "Whether to enable the output." ; 55default = true ; 56}; 57 58crtc = mkOption { 59type = types . nullOr types . ints . unsigned ; 60description = "Output video display controller." ; 61default = null ; 62example = 0 ; 63}; 64 65primary = mkOption { 66type = types . bool ; 67description = "Whether output should be marked as primary" ; 68default = false ; 69}; 70 71position = mkOption { 72type = types . str ; 73description = "Output position" ; 74default = "" ; 75example = "5760x0" ; 76}; 77 78mode = mkOption { 79type = types . str ; 80description = "Output resolution." ; 81default = "" ; 82example = "3840x2160" ; 83}; 84 85rate = mkOption { 86type = types . str ; 87description = "Output framerate." ; 88default = "" ; 89example = "60.00" ; 90}; 91 92gamma = mkOption { 93type = types . str ; 94description = "Output gamma configuration." ; 95default = "" ; 96example = "1.0:0.909:0.833" ; 97}; 98 99rotate = mkOption { 100type = types . nullOr ( types . enum [ "normal" "left" "right" "inverted" ]); 101description = "Output rotate configuration." ; 102default = null ; 103example = "left" ; 104}; 105 106transform = mkOption { 107type = types . nullOr ( matrixOf 3 3 types . float ); 108default = null ; 109example = literalExpression '' 110[ 111[ 0.6 0.0 0.0 ] 112[ 0.0 0.6 0.0 ] 113[ 0.0 0.0 1.0 ] 114] 115'' ; 116description = '' 117Refer to 118{manpage}`xrandr(1)` 119for the documentation of the transform matrix. 120'' ; 121}; 122 123dpi = mkOption { 124type = types . nullOr types . ints . positive ; 125description = "Output DPI configuration." ; 126default = null ; 127example = 96 ; 128}; 129 130scale = mkOption { 131type = types . nullOr ( types . submodule { 132options = { 133method = mkOption { 134type = types . enum [ "factor" "pixel" ]; 135description = "Output scaling method." ; 136default = "factor" ; 137example = "pixel" ; 138}; 139 140x = mkOption { 141type = types . either types . float types . ints . positive ; 142description = "Horizontal scaling factor/pixels." ; 143}; 144 145y = mkOption { 146type = types . either types . float types . ints . positive ; 147description = "Vertical scaling factor/pixels." ; 148}; 149}; 150}); 151description = '' 152Output scale configuration. 153 154Either configure by pixels or a scaling factor. When using pixel method the 155{manpage}`xrandr(1)` 156option 157`--scale-from` 158will be used; when using factor method the option 159`--scale` 160will be used. 161 162This option is a shortcut version of the transform option and they are mutually 163exclusive. 164'' ; 165default = null ; 166example = literalExpression '' 167{ 168x = 1.25; 169y = 1.25; 170} 171'' ; 172}; 173 174filter = mkOption { 175type = types . nullOr ( types . enum [ "bilinear" "nearest" ]); 176description = "Interpolation method to be used for scaling the output." ; 177default = null ; 178example = "nearest" ; 179}; 180}; 181}; 182 183hookType = types . lines ; 184 185globalHooksModule = types . submodule { 186options = { 187postswitch = mkOption { 188type = types . attrsOf hookType ; 189description = "Postswitch hook executed after mode switch." ; 190default = { }; 191}; 192 193preswitch = mkOption { 194type = types . attrsOf hookType ; 195description = "Preswitch hook executed before mode switch." ; 196default = { }; 197}; 198 199predetect = mkOption { 200type = types . attrsOf hookType ; 201description = '' 202Predetect hook executed before autorandr attempts to run xrandr. 203'' ; 204default = { }; 205}; 206}; 207}; 208 209profileHooksModule = types . submodule { 210options = { 211postswitch = mkOption { 212type = hookType ; 213description = "Postswitch hook executed after mode switch." ; 214default = "" ; 215}; 216 217preswitch = mkOption { 218type = hookType ; 219description = "Preswitch hook executed before mode switch." ; 220default = "" ; 221}; 222 223predetect = mkOption { 224type = hookType ; 225description = '' 226Predetect hook executed before autorandr attempts to run xrandr. 227'' ; 228default = "" ; 229}; 230}; 231}; 232 233hookToFile = folder :name :hook : 234nameValuePair "autorandr/ ${ folder } / ${ name } " { 235source = " ${ pkgs . writeShellScriptBin "hook" hook } /bin/hook" ; 236}; 237profileToFiles = name :profile : 238with profile ; 239mkMerge ([ 240{ 241"autorandr/ ${ name } /setup" . text = concatStringsSep "\n" 242( mapAttrsToList fingerprintToString fingerprint ); 243"autorandr/ ${ name } /config" . text = 244concatStringsSep "\n" ( mapAttrsToList configToString profile . config ); 245} 246( mkIf ( hooks . postswitch != "" ) 247( listToAttrs [ ( hookToFile name "postswitch" hooks . postswitch ) ])) 248( mkIf ( hooks . preswitch != "" ) 249( listToAttrs [ ( hookToFile name "preswitch" hooks . preswitch ) ])) 250( mkIf ( hooks . predetect != "" ) 251( listToAttrs [ ( hookToFile name "predetect" hooks . predetect ) ])) 252]); 253fingerprintToString = name :edid :" ${ name } ${ edid } " ; 254configToString = name :config : 255if config . enable then 256concatStringsSep "\n" ([ "output ${ name } " ] 257++ optional ( config . position != "" ) "pos ${ config . position } " 258++ optional ( config . crtc != null ) "crtc ${ toString config . crtc } " 259++ optional config . primary "primary" 260++ optional ( config . dpi != null ) "dpi ${ toString config . dpi } " 261++ optional ( config . gamma != "" ) "gamma ${ config . gamma } " 262++ optional ( config . mode != "" ) "mode ${ config . mode } " 263++ optional ( config . rate != "" ) "rate ${ config . rate } " 264++ optional ( config . rotate != null ) "rotate ${ config . rotate } " 265++ optional ( config . filter != null ) "filter ${ config . filter } " 266++ optional ( config . transform != null ) ( "transform " 267+ concatMapStringsSep "," toString ( flatten config . transform )) 268++ optional ( config . scale != null ) 269(( if config . scale . method == "factor" then "scale" else "scale-from" ) 270+ " ${ toString config . scale . x } x ${ toString config . scale . y } " )) 271else '' 272output ${ name } 273off 274'' ; 275 276in { 277options = { 278programs . autorandr = { 279enable = mkEnableOption "Autorandr" ; 280 281hooks = mkOption { 282type = globalHooksModule ; 283description = "Global hook scripts" ; 284default = { }; 285example = literalExpression '' 286{ 287postswitch = { 288"notify-i3" = "''${pkgs.i3}/bin/i3-msg restart"; 289"change-background" = readFile ./change-background.sh; 290"change-dpi" = ''' 291case "$AUTORANDR_CURRENT_PROFILE" in 292default) 293DPI=120 294;; 295home) 296DPI=192 297;; 298work) 299DPI=144 300;; 301*) 302echo "Unknown profile: $AUTORANDR_CURRENT_PROFILE" 303exit 1 304esac 305 306echo "Xft.dpi: $DPI" | ''${pkgs.xorg.xrdb}/bin/xrdb -merge 307''' 308}; 309} 310'' ; 311}; 312 313profiles = mkOption { 314type = types . attrsOf profileModule ; 315description = "Autorandr profiles specification." ; 316default = { }; 317example = literalExpression '' 318{ 319"work" = { 320fingerprint = { 321eDP1 = "<EDID>"; 322DP1 = "<EDID>"; 323}; 324config = { 325eDP1.enable = false; 326DP1 = { 327enable = true; 328crtc = 0; 329primary = true; 330position = "0x0"; 331mode = "3840x2160"; 332gamma = "1.0:0.909:0.833"; 333rate = "60.00"; 334rotate = "left"; 335}; 336}; 337hooks.postswitch = readFile ./work-postswitch.sh; 338}; 339} 340'' ; 341}; 342}; 343}; 344 345config = mkIf cfg . enable { 346assertions = flatten ( mapAttrsToList ( profile : 347{ config , ...} : 348mapAttrsToList ( output :opts :{ 349assertion = opts . scale == null || opts . transform == null ; 350message = '' 351Cannot use the profile output options 'scale' and 'transform' simultaneously. 352Check configuration for: programs.autorandr.profiles. ${ profile } .config. ${ output } 353'' ; 354}) config ) cfg . profiles ); 355 356home . packages = [ pkgs . autorandr ]; 357xdg . configFile = mkMerge ([ 358( mapAttrs' ( hookToFile "postswitch.d" ) cfg . hooks . postswitch ) 359( mapAttrs' ( hookToFile "preswitch.d" ) cfg . hooks . preswitch ) 360( mapAttrs' ( hookToFile "predetect.d" ) cfg . hooks . predetect ) 361( mkMerge ( mapAttrsToList profileToFiles cfg . profiles )) 362]); 363}; 364 365meta . maintainers = [ maintainers . uvnikita ]; 366}